10

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?

Jim
  • 10,120

3 Answers3

12

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
  • Is this flock -n /var/lock/myjob.lock part of a standard cron entry? – Jim Apr 08 '14 at 17:59
  • @Jim No, it's something you can use to ensure that only one job runs at a time. – Gilles 'SO- stop being evil' Apr 08 '14 at 19:05
  • Wow never knew it was this easy! Thanks for sharing. I was using a very long script to do this. My question is, is there a way to make the flock expire after, say, 1 hour and kill the first process and start a new one too? (without using a long complicated script) – supersan Feb 02 '21 at 07:24
  • 1
    @supersan timeout 1h flock -n /path/to/lock /path/to/script – Gilles 'SO- stop being evil' Feb 02 '21 at 08:35
  • This is mind blowing. I was using such a complicated script for doing this. There exists a command for everything in unix (and you seem to know it @Mr.Giles)! Wow. thank you! – supersan Feb 02 '21 at 12:34
0

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.

0

https://www.timkay.com/solo/solo

... that prevents a program from running more than one copy at a time. It is useful with cron to make sure that a job doesn't run before a previous one has finished. (docs: https://www.timkay.com/solo/)

Example

* * * * * solo -port=3801 ./jobA.sh blah blah

When jobA.sh take longer than 1 minute, cron will call solo again, but because port 3801 already in use, solo will return error, and will not execute jobA.sh.

  • 2
    If it is only 10 lines of code, then you would be able to include it in your answer. – Kusalananda Apr 16 '20 at 06:25
  • What does the -port=3801 part do? Doesn't this run every minute, while the question has an (example) time of 5 minutes? What happens if two jobs collide? Does it delay, or cause an error? – Jeff Schaller Apr 16 '20 at 18:22
  • port is important because if the task is not complete yet then solo port called again, the scropt job.pl not executed again. – Erlang Parasu Apr 17 '20 at 00:56
  • the idea is: because port 3801 is already in use.. solo will return error & the script job.pl not executed again.. when job.pl exit, then solo will exit too and release port 3801 and can be used by other program. – Erlang Parasu Apr 17 '20 at 01:01
  • @JeffSchaller you can use any port.. For example 9001 for jobA, 9002 for jobB.. – Erlang Parasu Apr 17 '20 at 01:27