Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 have a cronjob for rsync which runs every 2 minutes, but sometimes takes longer then 2 minutes. For that I wrote a simple locking system which checks, if file not found writes lockfile, takes action and deletes that lockfile.

Now if the script crashes or is killed for some reason that lockfile will linger on and cause problems.

What would be a good way of making sure the cron task is run again at some point? I have thought of checking the age of the lockfile and deleting it if older then a certain period, but there ought to be better more elegant solutions for this I'd think..


I now have implemented flock. I was a bit confused as to why the file always seems to exist, but I found this page that explains how it works, by storing the pid in the file information:

share|improve this question
up vote 5 down vote accepted

Store the PID of the critical process as part of the lock and when you run the script again check to see if the process is still active.

A better way is to use a lock directory rather than a lock file as mkdir is an atomic operation. You don't have to check if the lock exists and then create it if not, which leaves a window of opportunity for something else to get the lock. Put the PID of the critical process in the lock directory as a file etc.

On Linux you can use the flock utility which handles all this for you.

share|improve this answer

Check if the process and the lockfile exists. If only one of them exists, something is wrong and must be handled correctly. E.g. If the lockfile exists and the process is not running, delete the lockfile and move on.

share|improve this answer

I know you have a your own locking system, but I will do it with fcron and exesev(false)

Can a job be executed several times simultaneously ?
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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