One instance at a time with PID file in Bash

Often times, I only want a script to run one instance at a time. For example, if the the script is copying files, or rsync’ing between systems, it can be disastrous to have two instances running concurrently, and this situation is definitely possible if you run the script from cron.

I figured out a simple way to make sure only one instance runs at a time, and it has the added benefit that if the script dies midway through, another instance will start – a drawback of just using lock files without a pid.

Without further ado, here’s my script:

#!/bin/bash
pidfile=/var/run/sync.pid
if [ -e $pidfile ]; then
    pid=`cat $pidfile`
    if kill -0 &>1 > /dev/null $pid; then
        echo "Already running"
        exit 1
    else
        rm $pidfile
    fi
fi
echo $ > $pidfile
 
#do your thing here
 
rm $pidfile

CC BY-SA 4.0 One instance at a time with PID file in Bash by Craig Andrews is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

7 thoughts on “One instance at a time with PID file in Bash

  1. I modified your script slightly. It works on Bash and does not create a ‘1’ file. Using the ‘f’ flag with ‘rm’ also prevents useless error messages.

    PID_FILE=”/var/run/my_prog.pid”

    ## Check to see if the program is running
    if [[ -e ${PID_FILE} ]]; then
    ## Capture the ‘pid’ number in ‘pid’ variable
    pid=$( ${PID_FILE}

    do something

    ## Remove the pid file if it exists
    if [[ -e ${PID_FILE} ]]; then
    rm -f ${PID_FILE}
    fi

  2. Hi,

    Your syntax is a bit wrong, when I ran the command the output of kill got redirected to the file 1.

    Here is the syntax that worked for me:

    if kill -0 $pid > /dev/null 2>&1; then
    echo “Already running”
    exit 1
    else
    rm $pidfile
    fi
    fi
    echo $$ > $pidfile

  3. Maybe we can try to modify your run_one_instance.sh script a bit to make it so that the can be any other script?

    No copy/paste that way, and makes it more interesting 🙂

  4. You should also check if they process_id that is running is actually the same script. The system could release the script and then re-issue that process_id to another program.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.