5

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?

3
  • Is your question about a single cron job possibly running longer than the interval set or are you talking about independent cron jobs?
    – gertvdijk
    Commented Dec 14, 2012 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
    Commented Dec 14, 2012 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
    Commented Dec 14, 2012 at 11:17

2 Answers 2

9

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
  • 2
    I think this is not the 'simplest' way. See this answer instead.
    – gertvdijk
    Commented Dec 14, 2012 at 11:19
  • @gertvdijk thanks for the hint, I improved my answer.
    – jofel
    Commented Dec 14, 2012 at 12:54
2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .