Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for stopping the overlapping of the Cron job

Tags:

cron

I need to stop the overlapping of cron jobs for example:If a cron job is scheduled at morning 2 o clock for DB backup and other cron job is scheduled at morning 7 o clock for DB backup again.So i need to stop the 7 o clock scheduled cron job if the DB backup for 2 o clock is not completed.

like image 782
Raghu Avatar asked Oct 21 '25 08:10

Raghu


1 Answers

This is what flock is for. From man flock:

...
The  third form is convenient inside shell scripts, and is usually used
       the following manner:

       (
         flock -n 9 || exit 1
         # ... commands executed under lock ...
       ) 9>/var/lock/mylockfile
...

In this case, flock will try to get a lock on fd 9 for /var/lock/myfile. If it can't (because the earlier job is still running), it will exit.

You need to put it in a script.

like image 88
jim Avatar answered Oct 27 '25 06:10

jim



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!