0

I have developed a script which should execute continuously and sequentially. Now on creating a cron job for this script, it keeps executing asynchronously.

1) I kept a while loop in script and thought of executing this script once so i used @reboot to execute the script once but if apache craches then this script will not start executing by its own ??

2)Using * * * * * cron it executes this script but creates multi-thread and all cron overlaps on the server eventually.

I have ran out of ideas how to execute a server script continuously and sequentially even if apache server restarts.

Mihir Patel
  • 230
  • 4
  • 22
  • 1
    Could you possible create a 'lock' file? Meaning, when your script starts up, it checks to see if this file exists. If the file does, then the script does not continue. Otherwise it'll create the file on starting the script and remove the file after done. Just an idea. – Kyle Dec 05 '12 at 14:43
  • What about if you get the script to talk to itself via database. I.E update a cell frequently, and so if your next precess tries to run, it'll realise that your script is already running and stop. also if the script runs and there has been more than a few seconds between the last update of that cell, then it'll know to continue – David Sigley Dec 05 '12 at 14:43
  • So i assume you suggesting me to use something like sockets or comet technology that listen a database...kind of we use for push notifications...right ? So will it execute the script for indefinite period and even if apache restarts ? – Mihir Patel Dec 05 '12 at 14:57

3 Answers3

1

You're asking for:

a script which should execute continuously and sequentially

That's the definition of a daemon. You can use upstart to easily create a daemon with your php code.

Here you can find a good article to explain how create a daemon with upstart and node.js (but is easy to adapt to a php script): http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/

m4t1t0
  • 5,669
  • 3
  • 22
  • 30
0

cron is for repeating jobs on a timed basis (min interval: 1 minute). It's intended for scripts that eventually exit and need to be restarted from scratch. Sounds like your script is essentially trying to be a daemon - started once, then left running permanently.

Instead of cron to repeatedly start new copies of the script, simply start your script ONCE via an init script, e.g. /etc/rc.local

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    But what if the apache craches ? will it start by its own. I don't it to execute manually everytime apache restarts.... – Mihir Patel Dec 05 '12 at 14:47
0

There are some ideas to deal with checking if a script is running in this question, and you could have a cron run every couple of minutes to ensure its running and starting it if not.

Community
  • 1
  • 1
Michoel
  • 834
  • 5
  • 16