我有一个非常简单的 Python 程序,名为 test.py,我用它来测试 Monit。 Monit 似乎无法检测到该程序正在运行。这是当前的上下文:
在 Proxmox LXC 容器中运行的 Ubuntu 22.04.3 LTS 上运行的 Monit 版本 5.31.0;我也尝试过 5.26.0 和 5.33.0
check process test MATCHING "/home/pi/monit/test.py"
start program = "/usr/bin/python3 /home/pi/monit/test.py >> /home/pi/monit/test.log 2>&1" timeout 30 seconds
stop program = "/usr/bin/pkill -f '/home/pi/monit/test.py'"
if does not exist then restart
PID文件(如果使用PID方法):
-rw-r--r-- 1 root root 6 Aug 10 16:37 test.pid
来自 Monit 日志文件:
[2023-08-18T13:40:15+0200] error : 'test' process is not running
[2023-08-18T13:40:15+0200] info : 'test' trying to restart
[2023-08-18T13:40:15+0200] info : 'test' start: '/usr/bin/python3 /home/pi/monit/test.py >> /home/pi/monit/test.log 2>&1'
[2023-08-18T13:40:45+0200] error : 'test' failed to start (exit status -1) -- Program '/usr/bin/python3 /home/pi/monit/test.py >> /home/pi/monit/test.log 2>&1' timed out after 30 s
[2023-08-18T13:41:46+0200] error : 'test' process is not running
[2023-08-18T13:41:46+0200] info : 'test' trying to restart
[2023-08-18T13:41:46+0200] info : 'test' start: '/usr/bin/python3 /home/pi/monit/test.py >> /home/pi/monit/test.log 2>&1'
[2023-08-18T13:42:16+0200] error : 'test' failed to start (exit status -1) -- Program '/usr/bin/python3 /home/pi/monit/test.py >> /home/pi/monit/test.log 2>&1' timed out after 30 s
[2023-08-18T13:43:17+0200] error : 'test' process is not running
请注意,Monit 报告进程没有运行,但实际上它正在运行。 Test.py 只需几毫秒即可启动。手动启动没有问题。
我尝试过的事情:
如何解决这个问题?
您的 python 脚本不会生成新进程。
Monit 启动您的脚本,但启动脚本不会在给定时间内结束,因此 Monit 尝试一次又一次地启动脚本。
您应该知道 Monit 仅使用有限的环境来执行 Initd 或 Systemd 等脚本。要获得更有用/合适的环境,请使用 shell 脚本启动 python 脚本并初始化环境,在脚本中设置 PYTHONPATH 等内容。