Search code examples
phpdaemon

PHP, re-run the code when finished


I'm developing a PHP-service which does numerous operations per customer, and I want this to run continuously. I've already taken a look at cron, but as far as I understood cron made it possible to run the code on set times. This can be a bit dangerous since we are dependant that the code has finished running before it starts over, and the time for each run may vary as the customer base increases. So refresh, cron or other timed intervals cant be done, as far as I'm aware.

So I'm wondering if you know any solutions where I can restart my service when it is finished, and under no circumstances make the re-run before all the code have been executed?

I'm sorry if this is answered before or is easily found on Google, I have tried to find something, but to no avail.

Edit: I could set timed intervals to be 1 hour, to be absolutely sure, but I want as little time as possible between each run.


Solution

  • Look at this: http://www.godlikemouse.com/2011/03/31/php-daemons-tutorial/

    What you need is a daemon that keeps running. There are more solutions than this while loop.

    The following I once used in a project: http://kvz.io/blog/2009/01/09/create-daemons-in-php/ , it's also a package for PEAR: http://pear.php.net/package/System_Daemon

    For more information, see the following SO links:

    What is a daemon: What is daemon? Their practical use? Usage with php? How to use: PHP script that works forever :)