0

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.

Luceos
  • 6,629
  • 1
  • 35
  • 65
Kugie
  • 33
  • 5
  • You can make some kind of deamon to run in the background and wait for tasks. – Vestimir Markov Mar 26 '13 at 13:28
  • 1
    you're looking for a ```daemon``` – Luceos Mar 26 '13 at 13:28
  • Like these comments say, create a daemon that runs your code. Then, use another shell script that runs via cron to check if the daemon is still running. If it's not, restart the daemon. You can set the shell script to run as often as needed within the limitation of cron. – nickb Mar 26 '13 at 13:30
  • @nickb there is existing software that does exactly what you propose, and even more, see my answer – SirDarius Mar 26 '13 at 13:41

4 Answers4

1

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 :)

Community
  • 1
  • 1
Luceos
  • 6,629
  • 1
  • 35
  • 65
  • 1
    Thank you, I will take a look at deamon, and from the page you linked it seems like it will do the trick. Cheers! – Kugie Mar 26 '13 at 13:37
0

Have you tried runnning the PHP script as a process. This here has more details http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/

Gatonye
  • 81
  • 5
0

If you do not want to learn how to code a daemon, I recommand using a software that manages processes in userland: Supervisor (http://supervisord.org/)

You just need to write a configuration file to specify which processes you want to run, and how. It is extremely simple to configure and it is very adaptable (you can force having only one instance of your process, or instead have a fixed number of instances... etc). It will also handle automatic restart in case your script crashes, and logging.

On the PHP side, just create a script that never quits, using a while(true) { ... } loop, and add an entry like this in supervisord's conf:

[program:your-script]
command=/usr/bin/php /path/to/your_script.php

I'm using that software in production for a few projects (to run ruby and php gearman asynchronous workers for websites).

SirDarius
  • 41,440
  • 8
  • 86
  • 100
  • Nice, also heard of it; will also try it out ;) – Luceos Mar 26 '13 at 13:41
  • Thanks, if deamon fails, I will give this a go :) – Kugie Mar 26 '13 at 13:42
  • @user1477205 do as you please :) although I would say, try supervisor, and write a daemon if it fails instead, it's more work to have one up and running than using this existing facility. – SirDarius Mar 26 '13 at 13:44
-1

Try to have a custom logic , where you can set the flag ON and OFF and in your CRON , you can check before running the code inside it. I wanted to suggested something like Queue based solution , once you get the entry , then run the logic of your processing . Which can be either daemon or cron. It will give more control if your task is OK to execute now . Edited it

Devesh
  • 4,500
  • 1
  • 17
  • 28