Search code examples
cron

Does cron job executing script cancel each other


I can't wrap my head around this concept

If a cron job is set to execute a particular script every 5 minutes and the task the script is performing happens to be very long

What will happen if cron job re execute the script after 5 minutes and the script is not done executing the prior task will this cause the prior task to abort and restart again

Or will the server know its suppose to queue it and wait for the prior task before re executing the script


Solution

  • It is neither. The two tasks overlap, and run at the same time.

    Cron jobs don't cancel each other unless they try to lock and access the same files, or the scripts have some other conflicts. You can of course make them cancel each other if you want, though.

    The server is none the wiser, and the tasks don't wait for previous executions unless you explicitly add such details, for example, by using flock or pgrep to check for previous executions still running.

    The following example entry in cron tab checks if the script is already running, and only runs if the script (cron.sh) is not already running.

    * * * * * /usr/bin/pgrep -f /path/to/cron.sh > /dev/null 2> /dev/null || /path/to/cron.sh