Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Cron-Instance/Mutex

Tags:

mutex

cron

I have to ensure that a cron script can only run with a single instance at the same time. Now I use a simple TempFile and check it on every scriptstart. The problem with it is, that the script cannot release these lock on abort/failure. I tried some other with pcntl_signal() and can catch ctrl+c and kill now, but no errors. Maybe you have a suggestion witch solution works for all cases?

Greetings

like image 446
Daniel Avatar asked Apr 30 '26 03:04

Daniel


1 Answers

http://bunwich.blogspot.co.uk/2012/08/run-only-single-instance-of-cron-job.html has a good solution:

LOCKFILE=/var/run/moodle/moodlecron.lock
set -e
(
    flock -n 200 
    trap "rm $LOCKFILE" EXIT 
    echo Add your commands here
) 200>$LOCKFILE
like image 117
Alice Purcell Avatar answered May 07 '26 08:05

Alice Purcell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!