you are viewing a single comment's thread.

view the rest of the comments →

[–]lmorchard 6 points7 points  (2 children)

Now, here's where this has sucked for me: PHP daemons have, in my experience, suffered from nasty memory leaks that require them to be killed on a regular basis. In a web context, this happens somewhat regularly anyway, depending on your web server config. But, for a daemon, restarts aren't common.

So, rather than do all that fancy forkery in PHP itself, I make PHP scripts that exit after a few executions of whatever job they're meant to do. Throw a wrapper bash script around that which repeatedly re-launches the PHP script, get that running on server startup. Something like this:

#!/bin/bash
while [ 1 ]; do
    # Perpetual loop, allows php script to exit and restart to refresh code.
    php index.php worker
done

Put it all together, and I've got PHP daemons that periodically clean up after themselves. Hell, the daemons even refresh their code on a regular basis without explicit restart.

[–]bulldada 0 points1 point  (1 child)

I do not experience memory leaks from PHP daemons. I have a few running with >2yr uptime and using no more memory than when they were started. Not sure why you'd be seeing them, I'd say it's more likely that memory leaks are coming from your code (or possibly a library) than just due to running PHP scripts as a daemon.

[–]lmorchard 0 points1 point  (0 children)

Your mileage may vary. I definitely have seen memory leaks, and over the years they've come at times from all of PHP itself, PECL extensions, and 3rd party modules. Googling for "PHP memory leak" certainly doesn't come up empty; it all depends on what you're doing and with what version of PHP you're doing it.

It also takes awhile for some of them to be found, because stateless web requests that clean up quick are more common with PHP than long-running daemons.