使用 Monit Ubuntu 20 监控 php 命令行工作器

使用 Monit Ubuntu 20 监控 php 命令行工作器

使用 pid 方法监控 php 脚本时出现问题。

我们有一个必须在服务器上保持运行的 Laravel 工作脚本:

php /data/git/site/directory/artisan queue:work --daemon当在命令行上运行时,它会保持终端打开并且不会输出任何内容,直到 Laravel 应用程序将输出推送给它 - 这不太可能在启动后 30 秒内完成。

我按照建议编写了一个 bash 脚本 (/usr/local/bin/artisanqueue):

#!/bin/bash
case $1 in
    start)
       echo $$ > /var/run/artisanqueue.pid;
       exec 2>&1 php /data/git/site/directory/artisan queue:work --daemon 1>/tmp/artisanqueue.out
       ;;
     stop)
       kill `cat /var/run/artisanqueue.pid` ;;
     *)
       echo "usage: artisanqueue {start|stop}" ;;
esac
exit 0

然后我们创建了 /etc/monit/conf.d/laravel,并尝试了两个版本,第一个是“匹配”

# artisanqueue
check process artisanqueue
    matching "artisan queue:work"
    start program = "/usr/local/bin/artisanqueue start"
    stop = "/usr/local/bin/artisanqueue stop"
    if 5 restarts within 5 cycles then timeout

第二个“with pidfile”按照建议https://mmonit.com/wiki/Monit/FAQ#pidfile

# artisanqueue
check process artisanqueue with pidfile /var/run/artisanqueue.pid
    start program = "/usr/local/bin/artisanqueue start"
    stop = "/usr/local/bin/artisanqueue stop"
    if 5 restarts within 5 cycles then timeout

在 monit.log 中

[UTC Oct 11 23:02:04] debug    : 'artisanqueue' process test failed [pid=93896] -- No such process
[UTC Oct 11 23:02:04] error    : 'artisanqueue' process is not running
[UTC Oct 11 23:02:04] info     : 'artisanqueue' trying to restart
[UTC Oct 11 23:02:04] debug    : 'artisanqueue' process test failed [pid=93896] -- No such process
[UTC Oct 11 23:02:04] debug    : 'artisanqueue' process test failed [pid=93896] -- No such process
[UTC Oct 11 23:02:04] info     : 'artisanqueue' start: '/usr/local/bin/artisanqueue start'
[UTC Oct 11 23:02:34] error    : 'artisanqueue' failed to start (exit status -1) -- Program '/usr/local/bin/artisanqueue start' timed out after 30 s
[UTC Oct 11 23:03:06] info     : 'artisanqueue' monitor on user request
[UTC Oct 11 23:03:06] info     : 'artisanqueue' monitor action done
[UTC Oct 11 23:03:08] debug    : 'artisanqueue' process test failed [pid=93987] -- No such process
[UTC Oct 11 23:03:08] error    : 'artisanqueue' process is not running
[UTC Oct 11 23:03:08] info     : 'artisanqueue' trying to restart
[UTC Oct 11 23:03:08] debug    : 'artisanqueue' process test failed [pid=93987] -- No such process
[UTC Oct 11 23:03:08] debug    : 'artisanqueue' process test failed [pid=93987] -- No such process
[UTC Oct 11 23:03:08] info     : 'artisanqueue' start: '/usr/local/bin/artisanqueue start'
[UTC Oct 11 23:03:38] error    : 'artisanqueue' failed to start (exit status -1) -- Program '/usr/local/bin/artisanqueue start' timed out after 30 s

同时查看进程,我们发现它正在运行,但 PID 号不同

ubuntu@dev-us:/data/git/site/directory$ ps faux | grep 'artisan queue'
ubuntu     94064  0.0  0.0   7704   652 pts/0    S+   23:03   0:00              \_ grep --color=auto artisan queue
root       94039  0.9  2.6 124340 50484 ?        Ss   23:03   0:00  \_ php /data/git/site/directory/artisan queue:work --daemon

我们可以确认 Monit 正在查看与当前相同的 PID 编号,/var/run/artisanqueue.pid但它在该过程的某个地方发生了变化,我假设是在重启时发生的,但我无法确认在哪里。

[UTC Oct 11 23:23:06] error    : 'artisanqueue' failed to start (exit status -1) -- Program '/usr/local/bin/artisanqueue start' timed out after 30 s

我如何才能在这里得到一致的结果?

我正在运行 Ubuntu 20.04.4 LTS 和 Monit 版本 5.26.0

相关内容