X 时间后杀死 CPU 使用率高的进程?

use*_*153 23 linux time kill cpu-usage

运行 Linux 我有几个进程偶尔会崩溃(游戏服务器),最终使用 100% cpu。

我正在寻找一个程序或脚本来按名称检查进程列表的 cpu 使用情况,如果它们处于 100% 超过 X 时间,例如 30 秒,则杀死它们。我尝试了 ps-watcher,但无法确定如何完成此操作。

仅仅以 100% 的使用率终止进程是行不通的,因为它会在正常操作期间短暂地击中该进程。

我还发现这个脚本似乎可以做我想做的事,但它仅限于一个过程:链接

任何帮助是极大的赞赏!

jos*_*chi 22

试试monit

您可以使用这样的配置来完成您的任务:

check process gameserver with pidfile /var/run/gameserver.pid
  start program = "/etc/init.d/gameserver start" with timeout 60 seconds
  stop program  = "/etc/init.d/gameserver stop"
  if cpu > 80% for 2 cycles then alert
  if cpu > 95% for 5 cycles then restart
  if totalmem > 200.0 MB for 5 cycles then restart
  if loadavg(5min) greater than 10 for 8 cycles then stop
  if failed port 12345 type tcp with timeout 15 seconds
    then restart
  if 3 restarts within 5 cycles then timeout
Run Code Online (Sandbox Code Playgroud)

有关此配置的详细信息可以在 monit 的文档中找到。