Preventing simultaneous cron jobs with Flock

Preventing simultaneous cron jobs with Flock

Cronjobs are tasks that are executed periodically and can be set via the crontab command. It is an easy way to execute tasks automatically. The basic explanation of cronjobs can be found in an earlier blog post on our website.

However, a cronjob can also cause a big problem for the server when the same task is active multiple times and at the same time. A common problem is that a new task is started while the old one is not yet fully completed.

For example, if jw has set up a cronjob that executes a task every 5 minutes and that task actually needs 7 minutes to complete, then at minute 5 the same task will be started again. This results in two identical tasks running at that moment. It can even get so out of hand that with incorrect settings a task can run more than 10 – or more – times simultaneously. You can imagine that this is not desirable. For example, it can possibly have the following consequences:

  1. Data corruption; because the same task is executed multiple times, this can lead to strange changes in your (database) data.
  2. A slow website; because all resources (system resources) of your server are called. After all, every running cronjob needs these and will reserve them.
Of course, it is possible that the script that is being executed already uses a mechanism to prevent concurrent processes. In that case, you do not need to use Flock. For scripts that do not have such a mechanism, there is the following solution:
Flock.

Our High Performance Web servers are equipped with the “flock” tool as standard. This tool ensures that the same cron job cannot be started multiple times. Flock does this by creating a temporary “lock” file. As long as this file is present, no new task will be started. When the command (the cron job) has been executed, Flock will delete the temporary file so that a new task can be started. It is easy to use. Below is an example where the standard Magento cron job is executed using Flock:
  1. */5 * * * * flock -xn "${TMP}cron.php.lock" php -q /var/hpwsites/<user>/website/html/webroot/cron.php >/dev/null 2>&1
The “-xn” parameter specifies which lock file should be created. This should be different for each active cronjob. In the example above we use a system variable ($TMP) in combination with the description “cron.php.lock” which results in the creation of the following lock file “/var/hpwsites/tmp/cron.php.lock”.

If you would like to know more about flock, you can use the following command to open the manual when logged into your HPW server.

man flock