Have cron wait for job to finish before re-launching

不打扰是莪最后的温柔 提交于 2020-01-21 05:30:15

问题


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?


回答1:


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.



回答2:


Locking is one way to achieve this, or you could use a program similar to Cron called The Fat Controller which I created to solve exactly this problem.

Rather than running every x minutes, you can set an interval to wait between one instance of the script ending and another one starting.

You can even go further and use exit codes to tell The Fat Controller not to wait and run the script again immediately if, for example, a script that processes a limited number of items knows it still has more processing still to do when it ends. This is a nice way to handle batch processing.

There are more use cases and examples on the website:

http://www.4pmp.com/fatcontroller/



来源:https://stackoverflow.com/questions/3652714/have-cron-wait-for-job-to-finish-before-re-launching

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!