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