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.

notes/avoid_duplicated_cron_jobs_with_flock.txt · Last modified: 2017/11/04 07:35 by admin
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Run by Debian Driven by DokuWiki