In my script /usr/local/bin/backup, that I call every hour from /etc/crontab, I use rsync to copy data to an off-site server. That all worked fine, even in cases where we had somewhat more new data than can be pushed out in an hour.

Last week someone copied an 11GB file on the data partition and when I found out the next day there were 14 rsync programs running in parallel, each of then getting no bandwidth and each probably working on the same huge file. I killed them all (before realising I should have kept the first one running), stopped the cron job and ran the backup script by hand.

I can write out a file in the script before starting rsync and check in the script if that file is already there to prevent backup from running in parallel. Is there an easier way of doing this?

My /etc/crontab entry:

5  *  *  *  *   root  /usr/local/bin/backup

There are different ways of doing this, but IMO the easiest is inserting flock before the command in the crontab file:

5  *  *  *  *   root  flock -n /var/lock/backup /usr/local/bin/backup

The /var/lock/backup file is the lock that flock uses and -n immediately makes the command fail if the lock already exists.

This could of course mean that if one backup takes 1 hour and 1 minute, that the next one starts 59 minutes later. If that is a problem you could look into using -x.

| improve this answer | |

On a recent OpenBSD installation (OpenBSD 6.7, since snapshots Apr 16 2020), you may use the optional -s option in the schedule itself to avoid overlapping instances of a job:

5 * * * * root -s /usr/local/bin/backup

or, in the root user's own crontab,

5 * * * * -s /usr/local/bin/backup

From the OpenBSD crontab(5) manual:

Commands may be modified as follows:


-s command Only a single instance of command will be run concurrently. Additional instances of command will not be scheduled until the earlier one completes.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.