Avoid duplicated cron jobs with flock

Sometimes a scheduled process can take longer than expected, and starting it twice could break things.

If you use cron to schedule a process, you can use flock to be sure that your script/program will not be started if a previous scheduled instance is still running.

Let's say you want to run offlineimap every two hours. Instead of simply scheduling in crontab the program (as vmail user) with:

* */2 * * * vmail offlineimap

you can run offlineimap through flock and use /tmp/offlineimap.lck as lock file with:

* */2 * * * vmail flock -xn /tmp/offlineimap.lck -c 'offlineimap'

This way, every two hours cron starts offlineimap: if the lock file is still around, it means that offlineimap is still synchronizing and the current execution is safely skipped.