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
  • 1
    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

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.

  • 2
    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

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


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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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