Score:1

Monitoring php command line worker with Monit Ubuntu 20

kr flag

Problems monitoring php script using pid method.

We have a Laravel worker script that must be kept running on the server:

php /data/git/site/directory/artisan queue:work --daemon when this is run on the command line it holds the terminal open and does not output anything until the Laravel app pushes output to it - this is unlikely to be within 30 seconds of start up.

I wrote a bash script as suggested (/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

Then we created /etc/monit/conf.d/laravel, and have tried two versions, first 'matching'

# 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

Second 'with pidfile' as suggested 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

In the 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

Simultaneously looking at processes we see it running but with a different PID number

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

We can confirm that Monit is looking at the same PID number as is present in /var/run/artisanqueue.pid but it changes somewhere in that process, I'm assuming when the restart happens, but I can't confirm where.

[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

How do I get a consistent result here?

I am running Ubuntu 20.04.4 LTS and Monit version 5.26.0

lutzmad avatar
ug flag
I don not know how artisan work, but you can use an addition timeout for the start command, see https://mmonit.com/monit/documentation/monit.html#SERVICE-METHODS. But in general the command used to start an application should end after the appliaction is up an running. If the application switch to a new process, it seems to me, you should use matching to find/monitor the process in the check process statement.
sixfive avatar
kr flag
Thanks, I have tried extending the timeout too, but no luck yet. When I run `php /data/git/site/directory/artisan queue:work` on the command line it only runs while the terminal is open.
lutzmad avatar
ug flag
Hmm, as long as you can not start artisan in a background process, you can not start artisan with Monit. Try something like "/usr/bin/nohup /usr/bin/php /path/to/artisan queue:work --daemon etc &" on the command line.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.