Back to home

How can I prevent Cronjobs from being executed in parallel?

There are cron jobs in many projects which get call quiet often. Usually those jobs have a short run time. However the run time can increase with a growing dataset or more load on the server. This can lead to a siltation where the same job is run multiple times in parallel.

For some tasks this behaviour is undesirable. In other cases this can lead to problems if the jobs create an additional load which in turn leads to slower run times and even more jobs in parallel being executed.

The command flock provides a simple solution to ensure that a cron job is not executed in paralell.

flock -n /tmp/ -c "/my/cron/job -with -arguments"

The -n option tells flock not to wait for the lock but to exit without executing the cronjob. /tmp/ is the path to the lockfile. You should chose different lock files for different cron jobs to ensure that they are independent. The -c option contains the job to be executed.

Should the same job be started again while the old one is still running the new one can not get the lock on the lock file and aborts without executing the actual job. On finishing the job the lock will be released again and the next execution of the job will be performed.

Didn't find what you were looking for?

Contact our support:

+41 44 637 40 40 Support Portal