Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 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
up vote 16 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

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

share|improve this answer

A very simple way of settup a lock:

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

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
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
Ideally you'd want to use advisory flock from a process that runs the command you want as a subtask. That way if they all die the flock is released automatically, which using the presence of a lock file doesn't do. Using a network port would work in a similar way - though that's a way smaller namespace, which is a problem. – Alex North-Keys Mar 19 '15 at 16:45

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

share|improve this answer

No need to install some fancy package:

pgrep -xf "$*" > /dev/null || "$@"

It's faster to write that script yourself than to run "apt-get install", isn't it? You might want to add "-u $(id -u)" to the pgrep to check for instances run by the current user only.

share|improve this answer

A simple solution from that worked for me was using mkdir. This is an easy way how to make sure that only one instance of your program is running. Create a directory with mkdir .lock which returns

  • true if the creation was successful and
  • false if the lock file exists, indicating that there is currently one instance running.

So this simple function did all the file locking logic:

if mkdir .lock; then
    echo "Locking succeeded"
    eval ;
    echo "Lock file exists. Program already running? Exit. "
    exit 1

echo "Program finished, Removing lock."
rm -r .lock
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.