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.

If I have a crontab job that runs, for instance, every hour, but that job may take more than an hour to complete, will the next job fail to run?

share|improve this question
    
Is your question about a single cron job possibly running longer than the interval set or are you talking about independent cron jobs? –  gertvdijk Dec 14 '12 at 10:51
    
@gertvdijk: Different instances of the same job. I just wanted to make sure that it would always run, even when the previous instance is still running. –  paradroid Dec 14 '12 at 11:13
    
Ah okay. Having two instances of the same job running is something you would want to avoid generally, so that's not too obvious from your question. Just for the other visitors bumping into this question and actually want to avoid it, see this answer on AskUbuntu. –  gertvdijk Dec 14 '12 at 11:17
add comment

2 Answers 2

up vote 4 down vote accepted

No, cron scripts run parallelly, if you do not implement some locking mechanism.

See Quick-and-dirty way to ensure only one instance of a shell script is running at a time and Correct locking in shell scripts? for possible solutions.

A simpler way is to use lockfile, like in this answer or the run-one package (see this answer) - thanks to gertvdijk for suggesting it.

share|improve this answer
1  
I think this is not the 'simplest' way. See this answer instead. –  gertvdijk Dec 14 '12 at 11:19
    
@gertvdijk thanks for the hint, I improved my answer. –  jofel Dec 14 '12 at 12:54
add comment

There's also whenjobs which aims to be "a powerful but simple cron replacement". From its manpage:

PRE FUNCTIONS

Before a job runs, you can arrange that a "pre" function is called. This function may decide not to run the job (by returning "false").

One use for this is to prevent a particular job from running if there is already an instance of the same job running:

job "only one"
pre (Whentools.one ())
every 10 seconds :
<<
  # Takes longer than 10 seconds to run, but 'Whentools.one ()'
  # will ensure only one is ever running.
  sleep 11
>>

(It also provides Whentools.max n, allowing n instances of the job to run. By default, it works like Cron.)

share|improve this answer
add comment

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.