Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am looking for a simple and generic solution that would allow you to execute any script or application in crontab and prevent it from running twice.

The solution should be independent on the executed command.

I assume it should look like lock && (command ; unlock) where lock will return false if there was another lock.

The second part would be like if it acquired the lock, run command and unlock after command is executed, even if it returns error.

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

Take a look at the run-one Install run-one package. From the manpage for the run-one command Manpage icon:

run-one is a wrapper script that runs no more than one unique instance of some command with a unique set of arguments.

This is often useful with cronjobs, when you want no more than one copy running at a time.

Like time or sudo, you just prepend it to the command. So a cronjob could look like:

  */60 * * * *   run-one rsync -azP $HOME example.com:/srv/backup

For more information and background, check out the blog post introducing it by Dustin Kirkland.

share|improve this answer
add comment

See also Tim Kay's solo, which performs locking by binding a port on a loopback address unique to the user:

http://timkay.com/solo/

share|improve this answer
add comment

A very simple way of settup a lock:

if mkdir /var/lock/mylock; then
  echo "Locking succeeded" >&2
else
  echo "Lock failed - exit" >&2
  exit 1
fi

A scripts which want to run needs te create the lock. If the lock exists, another script is busy, so the first script can't run. If the file don't exists, no script has acquired the lock. So the current script acquires the lock. When the script has finished the lock needs to be realeased by removeing the lock.

For more information about bash locks, check this page

share|improve this answer
1  
You'll also want an EXIT trap that removes the lock on exit. echo "Locking succeeded" >&2; trap 'rm -rf /var/lock/mylock' EXIT –  geirha May 25 '12 at 10:23
add comment

Your Answer

 
discard

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.