Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to run a cron every 5 minutes, but I don't want the next cron to run if the previous did not finish yet.

What can be done about this? Are there any tools to support this?


share|improve this question

closed as off-topic by mdpc, Bill Weiss, Jenny D, Dennis Kaarsemaker, Iain Apr 30 '14 at 16:08

  • This question does not appear to be about server, networking, or related infrastructure administration within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

possible duplicate of How to schedule server jobs more intelligently than with cron? – Iain Apr 30 '14 at 16:08
up vote 11 down vote accepted

Use flock.

flock -n /path/to/lockfile -c "command_you_want_to_run"
share|improve this answer
1. will the file be released after command_you_want_to_run? 2. how can that be integrated with a cron? ****5 flock -n .... is that right? Thanks – Shazam Apr 29 '14 at 19:15
1: Yes. 2: Yes. – sciurus Apr 29 '14 at 19:25

You can use the run-one command. It automatically creates a lock file for the command being run.

run-one rsync -a /my/hd

The behavior is similar to flock, but the syntax is more clear. The downside is that you might need to install the command to use it. Also, special attention should be paid when modifying the command line, when the command line changes, so does the lock file name

share|improve this answer

Cron does not support any type of job dependancies. The entries you put in there run when specified.

You are going to have to put in some simple interlocking scheme. Putting the given command or set of commands in a wrapper script containing the locking scheme.

Or as larger sites do, install some type of job scheduling applications and use that to run the applications with conditions.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.