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.
Did this help you out? Like what you see?
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!
☕️ Buy me a Coffee ($3)
PHP is meant to die. It doesn’t mean that it will disappear into nothingness, it just means that your PHP code can’t run forever; The core PHP feature follows the most simple programming workflow: get input data, process it, display the output, and die.
If you want to keep [PHP] processes running forever they will crash, and will do it really fast under load, because of known or unknown reasons. That’s nothing you can really control, it’s because PHP is meant to die. The basic implementation, the core feature of the language, is to be suicidal, no matter what.
Really interesting read (with follow-up) for anyone doing PHP development involving continously running PHP scripts. Highly recommended.
Been running a WebSocket Server — implemented via the excellent Ratchet which is built on top of the aforementioned React — for a few days now and with each connection that connects & closes a tad of memory is left occupied indeed (viz. opening and closing a connection doesn’t result in the same memory usage as before the connection was created) … looks like I’ll have to keep an eye on this.
PHP is meant to die →
PHP is meant to die, continued →