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.

3
  • 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. Commented Dec 5, 2012 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 Commented Dec 5, 2012 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 ? Commented Dec 5, 2012 at 14:57

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

But what if the apache craches ? will it start by its own. I don't it to execute manually everytime apache restarts....
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.