Wednesday, August 4, 2010

Simple cron locking system

Run cron jobs safely, using locking. Cron lock prevents running a cronjob if the first one didn't finished running (prevents cron overlaps).

2 files are used:
- cron-job1.lock

Contents of cron-job1.lock will be:
- free - if it's free, is NOT running
- locked - if is running

Locking script (



lock_content=`cat $lock_file`

if [[ "$lock_content" == "free" ]] ; then

    echo "locked" > $lock_file
    # do stuff here:
    cd /some-dir
    php compute-things.php
    php email-results.php

    echo "free" > $lock_file

    echo "script already running, file $lock_file is not free"

No comments: