Monit

為什麼monit抱怨無法讀取pid文件?

  • September 22, 2020

我剛開始監視並且對 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'

只是為了透明:我也在 monit-general@nongnu.org 郵件列表上發布了同樣的問題,但它似乎不是很活躍。我將同步兩個執行緒之間的任何回复!

正確的語法是“檢查程序”(https://mmoni.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

引用自:https://serverfault.com/questions/1034804