Hello, please sign in or register
You are here: Home

BASH pid detection

This detects whether an instance of the file is already running and exits the script if it is.

I've modified Jonathon Franzone code, removing the "." around the file names because it didn't work and also making the script exit when an instance is already running. This script can be added with "source" to any bash script.

#!/bin/bash
# This script should be included at the top of BASH scripts to prevent multiple instances of the script
# e.g. write at the top of the script
# 	source ./pid.sh

# Store PID of script:
# Match script without arguments
LCK_FILE=`basename $0`.lck

if [ -f "${LCK_FILE}" ]; then

  # The file exists so read the PID
  # to see if it is still running
  MYPID=`head -n 1 $LCK_FILE`

  if [ -n "`ps -p ${MYPID} | grep ${MYPID}`" ]; then
    echo `basename $0` is already running [$MYPID].
    exit
  fi
fi

# Echo current PID into lock file
echo $$ > $LCK_FILE


e.g.

Here's my test script using the source command to include the ./pid.sh file to the current script

#!/bin/bash

source ./pid.sh

echo "Starting"
sleep 5
echo "completed"

And i ran the script twice see print out of command line

server ~ # ./test.sh &
[1] 556
pluto ~ # Starting

server ~ # ./test.sh &
[2] 567
server ~ # test.sh is already running [556].
completed

This created a file in my current working directory called test.sh.lck

See http://www.ss64.com/bash/period.html for information on bash source command

Match Arguments also

To match a script instance with arguments. Replace the line LCK_FILE=`basename $0`.lck with...

# Match script without arguments
# LCK_FILE=`basename $0`.lck

# Match script with arguments $@
LCK_FILE=`basename $0`.`echo "$@" | sed "s/[\/\ \'\"]/_/"`.lck

In this case you can't attach parameters when calling pid.sh e.g.

# AVOID THIS
source ./pid.sh -arg

As this will override the arguments available in ./pid.sh through $@

Queuing Processes

Another useful action is to queue the process until another instance of the script has completed

For this i've rewritten the above script using the `while` loop and `sleep 1` checking for the parameter "--queue".

The following script is an enhancement of the above and can be used to exit or to queue the current instance of a script, as desired.

#!/bin/bash
#
# Queue the script (you can't match arguments too)
# e.g. 
#	source ./pid.sh --queue
#

# Match script without arguments
# LCK_FILE=`basename $0`.lck

# Match script and arguments $@
LCK_FILE=`basename $0`.`echo "$@" | sed "s/[\/\ \'\"]/_/"`.lck

# Am I Running
if [ -f "${LCK_FILE}" ]; then

  # The file exists so read the PID
  # to see if it is still running
  MYPID=`head -n 1 $LCK_FILE`

  while [ -n "`ps -p ${MYPID} | grep ${MYPID}`" ]
  do
     if [ "$1" == "--queue" ]; then
        echo "Queuing"
        sleep 1
        MYPID=`head -n 1 $LCK_FILE`
     else
        echo `basename $0` is already running [$MYPID].
        exit
     fi
  done 
fi

# Echo current PID into lock file
echo $$ > $LCK_FILE

To queue a script if another instance is already running add this line

source ./pid.sh --queue

 E.g. ./test.sh now becomes

#!/bin/bash

source ./pid.sh --queue

echo "Starting"
sleep 5
echo "completed"

This will output

server ~ # ./test.sh &
[1] 25344
server ~ # ./test.sh & Starting

[2] 25356
server ~ # Queuing
Queuing
Queuing
completed
Starting
completed

Comments

Ken Fallon
Thanks Drew. A very handy addition to scripts. Ken.
Created 11/08/09
Please don't do it this way
There's a race condition here. If you have two copies of this running nearly simultaneously, then both of them might initially see that the lock file is non-existent, and both of them will write their PID into it and carry on, A far more reliable wa...
Created 14/10/09
Stephen Thomas
I'm perplexed by this comment editor! Anyway have a look at http://flabdablet.nfshost.com/linux-scripts/test-locking.sh
Created 14/10/09
Thank you so much
Your detailed notes and well explained scripts for this really saved me time white solving a time pressing problem :)
Created 05/06/14
wanglili
air jordan retro coach outlet online coach outlet...
Created 21/08/17
meadc
Oakley Sunglasses hermes belt
Created 09/09/17
Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share