Avoid script running multiple times by filelock

Sometimes we need a single instance of a script to run at a time. Meaning, the script itself should detects whether any instances of himself are still running and act accordingly.

When multiple instances of one script running, it�s easy to cause problems. I�ve ever seen that about 350 instances of a status checking script running there without doing anything, but eat lots of system resource.

It's simple to implement it in Bash Shell:

# This is to examine whether the lockfile existed
[ -f "${0}.lock" ] && exit -1
# Create the lock file
lockfile ${0}.lock
sleep 40
# Release the lock file manually
rm -f ${0}.lock

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

(Hi, Mod, thanks for your note, I'd pay attention next time)

# This is to examine whether the lockfile existed
[ -f "${0}.lock" ] && exit -1
# Create the lock file
lockfile ${0}.lock
sleep 40
# Release the lock file manually
rm -f ${0}.lock

The first test is superfluous as the lockfile command will "wait" untill the lock is released by the other process. By default it will retry forever every 8 seconds (default sleeptime). If you want the script to exit instead of waiting, just define retry 0.

Example, first process:

lockfile /tmp/lock; sleep 10; rm -f /tmp/lock

Second process will wait if first process is busy

lockfile /tmp/lock && echo "Finaly, I'am running..."; rm -f /tmp/lock

Second process abort if first one is busy:

lockfile -r0 /tmp/lock && echo "Finaly, I'am running..."; rm -f /tmp/lock

Thanks, Ripat, nice examples, so the code can be eased like this

# Create the lock file
lockfile -r0 ${0}.lock && {
     # Your code goes here!
}
# Release the lock file manually
rm -f ${0}.lock

---------- Post updated at 10:12 PM ---------- Previous update was at 10:12 AM ----------

Here's an alternative way to lock the file:

lock_on()
{
    local f=$1
    local freefd=6  ## do not use fd 5

    ## make sure the file be there
    mkdir -p "$( dirname $f )"
    touch "$f"

    ## find a free fd
    while (( freefd <= 9 )); do
        [[ -L /dev/fd/$freefd ]] || break
        (( freefd++ ))
    done

    (( freefd == 10 )) && return 1

    ## open the lock file
    eval "exec $freefd< \"$f\""
}

is_locked()
{
    local f=$1

    fuser "$f" &> /dev/null
}

lock="/tmp/.$( basename $0 ).lock"
is_locked "$lock" && exit 1
lock_on "$lock"
## do something here