Running a PHP script forever with Supervisor
Let’s say you want to run a script forever (like a job queuing system that needs to constantly listen to incoming jobs) you could just start the process with a php my-script.php
and be done with it (HAHA jk) but then how do you make sure it always run? And what if you want to monitor it?
I saw in the past people relying on crontabs (@reboot) and doing a screen of their scripts. the problem with this approach is that your script might die for whatever reason and won’t restart.
I stumbled upon a nice process control system called Supervisor which will make sure your scripts run and will even give you a nice web interface :)
Install Supervisor
The above command will install the supervisor service in your /etc/
that you can start, stop, restart, force-reload, status, force-stop.
If you look at the /etc/supervisor/supervisord.conf
config file, you’ll see that supervisor includes config files in /etc/supervisor/conf.d/*.conf
. Let’s create one for our forever running php script.
And now let’s create our dummy php script file:
I know - awesome script right?
So let’s save all that and restart our supervisor:
You should see the python process running (/usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
) and your awesome (haha) script (\_ php /var/www/areWeThereYet.php
) also running in the background, populating your two logs defined in the supervisor config.
Web interface of logs
Restart supervisor and now if you have a http server (restart it as well) you can go on http://localhost:9001 fill in username/password and access the supervisord web interface.
Supervisor web interface