MikeN
MikeN

Reputation: 46357

Keep crontab jobs from overrunning each other?

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?

Upvotes: 4

Views: 322

Answers (3)

Timur Shtatland
Timur Shtatland

Reputation: 12425

Use systemd (specifically, systemd.timer) as an alternative to cron/flock, as explained in this answer on Server Fault.

The above question and a few of its answers also discuss cron and flock in more details. They expand on the suggestions by dqhendricks and marqueed in their answers to your question.

See also: Prevent duplicate cron job running.

Upvotes: 0

marqueed
marqueed

Reputation: 1123

Use flock. It basically does what dqhendricks suggests.

Upvotes: 1

dqhendricks
dqhendricks

Reputation: 19251

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!

Upvotes: 2

Related Questions