我想使用 monit 杀死一个使用超过 X% CPU 超过 N 秒的进程。
我正在使用stress
生成负载来尝试一个简单的示例。
我的 .monitrc:
check process stress
matching "stress.*"
if cpu usage > 95% for 2 cycles then stop
我开始监视(我检查了语法monit -t .monitrc
):
monit -c .monitrc -d 5
我发起压力:
stress --cpu 1 --timeout 60
压力显示top
为使用 100 % CPU。
我希望 monit 在大约 10 秒内消除压力,但压力会成功完成。我究竟做错了什么?
我也试过monit procmatch "stress.*"
了,由于某种原因显示了两个匹配项。也许这有关系?
List of processes matching pattern "stress.*":
stress --cpu 1 --timeout 60
stress --cpu 1 --timeout 60
Total matches: 2
WARNING: multiple processes matched the pattern. The check is FIRST-MATCH based, please refine the pattern
编辑:尝试了 e.lopez 的方法
我不得不从 .monitrc 中删除 start 语句,因为它导致了 monit 错误('stress' failed to start (exit status -1) -- Program /usr/bin/stress timed out
然后是僵尸进程)。
所以手动启动了压力:
stress -c 1
stress: info: [8504] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
.monitrc:
set daemon 5
check process stress
matching "stress.*"
stop program = "/usr/bin/pkill stress"
if cpu > 5% for 2 cycles then stop
启动监控:
monit -Iv -c .monitrc
Starting Monit 5.11 daemon
'xps13' Monit started
'stress' process is running with pid 8504
'stress' zombie check succeeded [status_flag=0000]
'stress' cpu usage check skipped (initializing)
'stress'
'stress' process is running with pid 8504
'stress' zombie check succeeded [status_flag=0000]
'stress' cpu usage check succeeded [current cpu usage=0.0%]
'stress' process is running with pid 8504
'stress' zombie check succeeded [status_flag=0000]
'stress' cpu usage check succeeded [current cpu usage=0.0%]
'stress' process is not running
'stress' trying to restart
'stress' start skipped -- method not defined
Monit 看到正确的进程(pids 匹配),但看到 0% 的使用率(压力是每个顶部 100% 使用 1 个 cpu)。我手动消除了压力,这是当 monit 说进程没有运行时(最后,上面)。因此,monit 可以正常监控进程,但没有看到正确的 cpu 使用情况。
有任何想法吗?