Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is there a way to run a script every minute (or 2, or 5, etc), but only if it isn't already running?

We have a set of scripts that need to run every minute. Sometimes they might start and finish in a second, other times they might go on for 5 minutes.

Our current way of avoiding simultaneous executions is by setting a is_running flag in each script, and exiting if it's still enabled. But this is a little unreliable (i.e., fatal errors would cause the flag to remain enabled even after the script halted).

We could write our own little manager, but I'm wondering if there is a more fashionable solution that already exists.

share|improve this question
    
+1 I am interested in this also. I do not know the answer, however I am interested in the possible solutions. –  Saif Bechan Apr 6 '10 at 16:16

4 Answers 4

a better way is to use flock instead of a pidfile. check the manpage: flock(1). The advantage is that no matter how a process finishes/dies, the lock is gone with it.

share|improve this answer

I would tend to agree with Warner's pid file answer. However, does the following feature of Anacron accomplish this?

-s
    Serialize execution of jobs. Anacron will not start a new job before the previous one finished. 

I haven't tested it myself, I don't find anacron's documentation thorough enough ...

If you want be particularly lazy ;-) You can just have the script exit if greping through ps output returns the process as running. But a lock/pid file is best.

share|improve this answer
    
anacron is a more recent version of cron, so it has more features than the old standard. about the 'how', it probably registers a SIGCHLD handler, which is triggered when a child process dies. thus it can keep track of running childs and simply skip those jobs. –  Javier Apr 6 '10 at 16:38
    
@ Javier: I disagree with "anacron is a more recent version of cron, so it has more features than the old standard." There is some overlap between what anacron and cron do, but they solve different problems. Using cron, how would you start a job that was missed because a machine was down? Using anacron, how would you start a job every December 25th or every 5 minutes? (You can, of course, script workarounds, but that's not what I'm talking about.) –  Dennis Williamson Apr 6 '10 at 16:54
    
@Kyle: That option causes different jobs scheduled to run at the same time to run one at a time. It might work for the OP's need, but it would block other jobs or other jobs would block it. Also, anacron doesn't run at periods shorter than a day. –  Dennis Williamson Apr 6 '10 at 16:58
    
@Dennis Williamson: i guess you're right, i haven't checked the history details. But still i guess you'll agree that anacron project started after cron was pretty much established, and is inspired by it; therefore it's not suprising that it has features that cron doesn't. Also, the original question was about cron –  Javier Apr 6 '10 at 17:59

That's the proper solution for that approach. Typically, a pid file would be used and a pid test would be done against the process to insure it's running. If stale, the lockfile would be removed and the process would run anyway.

Any additional intelligence would typically be written in the software itself, as a daemon, as opposed to running in cron.

share|improve this answer

If your scripts are coded in a language which supports the flock(2) syscall, then you could flock() a lock file with a function call too. Example:

If you are stuck with Bash or other scripting language which doesn't support this, the already proposed solution with flock(1) is fine too.

P.S. In any case, you should create a separate lock file only once (if it doesn't already exist), and never delete it. The directory "/var/lock" is a good place for such files.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.