Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have cron wait for job to finish before re-launching

Tags:

cron

I have a cronjob that executes every second minute that usually runs in seconds, but sometimes for several minutes.

I need cron to not execute the command if it's already running when the next minute comes.

The line looks like this */1 * * * * cmd

I have tried with this * * * * * ID=job1 FREQ=1m AFTER=job1 cmd

but to no success.

Is it possible to solve with cron or do I have to implement locking?

like image 280
Fredrik Jansson Avatar asked Nov 20 '25 21:11

Fredrik Jansson


1 Answers

You can make a temp file called inProgress (or whatever) and store it in a standard place, and use this to communicate to the next job if it should run or not.

What if flow of the job goes like this:

  1. Check for a standard inProgress file
  2. If it exists, quit
  3. Else, create inProgress file
  4. Do work
  5. Delete inProgress file.
like image 161
bwawok Avatar answered Nov 22 '25 17:11

bwawok



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!