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?
-
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
-
1Ah 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
2 Answers
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.
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.)

- 15,384