Monit 未检测到程序正在运行

问题描述 投票:0回答:1

我有一个非常简单的 Python 程序,名为 test.py,我用它来测试 Monit。 Monit 似乎无法检测到该程序正在运行。这是当前的上下文:

在 Proxmox LXC 容器中运行的 Ubuntu 22.04.3 LTS 上运行的 Monit 版本 5.31.0;我也尝试过 5.26.0 和 5.33.0

  • 我已经使用 Monit 很多年了,但从未使用过这个系统。
  • Monit 成功启动程序,但在指定的超时时间后尝试重新启动程序
  • Monit 配置的相关部分:
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 只需几毫秒即可启动。手动启动没有问题。

我尝试过的事情:

  • 不同版本 Monit 的多个全新安装
  • 在裸机 Ubuntu 22.04.3 LTS 上运行
  • 使用PID文件以及MATCHING方法
  • “启动程序”语法的多种变体

如何解决这个问题?

monit
1个回答
0
投票

您的 python 脚本不会生成新进程。

Monit 启动您的脚本,但启动脚本不会在给定时间内结束,因此 Monit 尝试一次又一次地启动脚本。

您应该知道 Monit 仅使用有限的环境来执行 Initd 或 Systemd 等脚本。要获得更有用/合适的环境,请使用 shell 脚本启动 python 脚本并初始化环境,在脚本中设置 PYTHONPATH 等内容。

© www.soinside.com 2019 - 2024. All rights reserved.