Home > Software design >  Ubuntu - Prevent crontab from starting new instance when the original script is still running
Ubuntu - Prevent crontab from starting new instance when the original script is still running

Time:04-27

On my Ubuntu 18.04 machine, I have a Python script that needs to run every x hours (let's say 3) and I use crontabfor that. But once the script starts, it can take more than x hours to finish (let's say 5 hours). This means that the next start is already due while the first one is still running. For various reasons, I cannot simply increase the time interval between script starts, but have to rely on that specific setup.

In the Windows task scheduler, there is an option that prevents starting a new instance in case the script is still running. Is there something similar for the Linux crontab?

CodePudding user response:

flock is a great solution for this if you can install it on your system.

Say you have the following in your crontab:

0 */3 * * * my_script.sh

You can add in a lock check via flock as follows:

0 */3 * * * flock /tmp/my_script.lock my_script.sh

There are options you can pass as well - for example, you can have flock wait up to 2 minutes for the prior lock to resolve as follows:

0 */3 * * * flock -w 120 /tmp/my_script.lock my_script.sh

If you can't install flock, then you could write your own "lock" file and remove it when done with a bit more work - or you can consider using pgrep.

Here's some more info on both flock and pgrep options

  • Related