Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep crontab jobs from overrunning each other?

Tags:

cron

If you have a crontab job that runs every 5 minutes, how do you ensure that it won't ever overrun itself? That is, how do you make sure that the previous iteration of the crontab job completed before the next one runs?

like image 811
MikeN Avatar asked Oct 25 '25 09:10

MikeN


2 Answers

at the beginning of your script, create a unique temp file somewhere in the file system, unless the file already exists, in which case your script is already running and you should exit. at the end of your script, delete the unique temp file.

there are other variations of this i'm sure, but they all have a similar idea. if you like this answer, please select the check mark next to it. thanks!

like image 176
dqhendricks Avatar answered Oct 28 '25 02:10

dqhendricks


Use flock. It basically does what dqhendricks suggests.

like image 37
marqueed Avatar answered Oct 28 '25 02:10

marqueed



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!