我对 monit 很陌生,并且对 pid 文件有一个疑问:我已将以下内容添加到我的 /etc/monit/monitrc 中:
cat /etc/monit/monitrc
set daemon 30
set logfile /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
set eventqueue
basedir /var/lib/monit/events # set the base directory where events will be stored
slots 100 # optionally limit the queue size
check ping.sh
with path "/path/to/ping.sh"
every "44 * * * *"
#if status != 0 then alert
我得到了以下信息/var/log/monit.log
:
[UTC Sep 21 22:44:09] error : Error reading pid from file '/path/to/ping.sh'
[UTC Sep 21 22:44:09] error : 'ping.sh' process is not running
[UTC Sep 21 22:44:09] info : 'ping.sh' trying to restart
尽管:
ls -l /path/to/ping.sh
-rwxrwxr-x 1 root root 1045 Sep 21 20:08 /path/to/ping.sh
在脚本内部,pid 存储在 /var/run/ping.pid 中:
#!/bin/bash
pidfile="/var/run/ping.pid"
# Get the pid of the currently running script
ps ax | grep $0 | grep $SHELL | awk '{print $1}'>$pidfile
使用以下命令在脚本底部删除 pid 文件:
rm $pid文件
为什么会出现错误:Error reading pid from file '/path/to/ping.sh'
?
坦白说:我已在[电子邮件保护]邮件列表也一样,但似乎不太活跃。我会同步两个主题之间的任何回复!
答案1
正确的语法是“检查程序”(https://mmonit.com/monit/documentation/monit.html#Program)
此外,不建议在 CRON 表达式中为分钟字段指定唯一值。对于你的情况,最好每 120 个周期触发一次检查(1 小时 -> 60 分钟 -> 每 30 秒 1 个周期 -> 每 120 个周期)
总结如下:
check program ping.sh with path "/path/to/ping.sh"
every 120 cycles
if status != 0 then alert