In a recent project I needed to permanently run a PHP CLI script; it needed to run as a service/daemon. A few Google search coupons later I got my answer: use
start-stop-daemon to make it work.
To not manually type in those lengthy commands it’s possible to wrap these in a handy little script, as shown in the gist embedded below:
“Installation” is straightforward: save the file into
/etc/init.d/myservice and make it executable by running
chmod +x /etc/init.d/myservice. Change
DAEMON_OPTS to reflect your setup.
Once done you can start the service by running
/etc/init.d/myservice start and stop it by running
/etc/init.d/myservice stop. Restarting is also possible by running
Verify the running of the daemon by checking the contents of the created PID file:
cat /var/run/myservice.pid — It should contain a process ID.
Related: PHP is meant to die
I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!