Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was wondering if we add a job in the crontab e.g. to run every 5 mins and the job does not actually manage to finish in 5 mins, does the cron daemon know that the previous instance is already running and skips the next run? Or do I have to somehow build that logic to the process? How?

share|improve this question
    
No: there is no such mechanism built in cron. You had to do it yourself. Usually lock are a good place to start (or the presence of a pid file for a naive and simplistic approach). –  Ouki Apr 7 at 21:39
2  
You have to wrap your job into an shell script which does the locking. See Correct locking in shell scripts? for possible options. –  jofel Apr 7 at 21:42
    
@Ouki: You should post that as an answer. –  Keith Thompson Apr 7 at 22:20
    
@Keith: being distracted from work ... but not enough to build a proper answer ;) –  Ouki Apr 7 at 23:25

2 Answers 2

No, the contract with cron is that it starts each job at the specified time. Cron doesn't keep track of which successive jobs are “the same job”.

If you want to avoid starting a job when the previous one isn't finished, you need to put something at the beginning of your job that makes it exit early. For example, you can arrange for your job to hold a lock file, and exit if it can't open the lock file.

* * * * flock -n /var/lock/myjob.lock /path/to/script
share|improve this answer
    
Is this flock -n /var/lock/myjob.lock part of a standard cron entry? –  Jim Apr 8 at 17:59
    
@Jim No, it's something you can use to ensure that only one job runs at a time. –  Gilles Apr 8 at 19:05

No, it does not.

What you can do is set the cron to run a script which contains your job and set it to run for a specific amount of time. Right before each time interval, terminate/kill that job from within that script and have cron restart it again at the moment of the next time interval.

share|improve this answer
    
How can I do what you describe? –  Jim Apr 10 at 20:43

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.