Making sure only one instance of a script is running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Making sure only one instance of a script is running
# 1  
Old 01-16-2017
Making sure only one instance of a script is running

so i have a script that takes a while to complete and its cpu intensive. this script is being used by several users. i want to make sure only 1 user can run this script at any given time.

i originally thought of running a while loop to egrep the process table of the PID ($$) of the process, but im not sure if that's going to be efficient. i want to make sure the script does not mistaking someone editing the script as a running process. so im excluding the common editing programs.

im searching for a solution that will be usable across all unix systems. here's what i have tried so far:

Code:
procname$(ps -ef | egrep "processname" | egrep -vc "grep| vi | ed | emacs")
if [ ${procname} -gt 0 ] ;then
echo "Script is currently in progress..aborting..."
exit 3
fi

# 2  
Old 01-16-2017
You could try using a .pid file and check it at the top of your script like this:

Code:
#!/bin/sh

NAME=$(basename "$0")

if [ -f /var/run/"$NAME.pid" ] &&
  ps -p $(cat /var/run/"$NAME.pid") >/dev/null 2>&1
then
   echo "Script is currently in progress..aborting..."
   exit 3
fi

trap 'rm -f /var/run/"$NAME.pid"' EXIT
echo $$ > /var/run/"$NAME.pid"

...

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-16-2017
Quote:
Originally Posted by Chubler_XL
You could try using a .pid file and check it at the top of your script like this:

Code:
#!/bin/sh

NAME=$(basename "$0")

if [ -f /var/run/"$NAME.pid" ] &&
  ps -p $(cat /var/run/"$NAME.pid") >/dev/null 2>&1
then
   echo "Script is currently in progress..aborting..."
   exit 3
fi

trap 'rm -f /var/run/"$NAME.pid"' EXIT
echo $$ > /var/run/"$NAME.pid"

...

All designs that try to check for a lock-file or pid-file are flawed. They are subjective to racecondition
The problem is that you can not check for the existence of a file and the creation of that file in an atomic action. And in between of those two actions another instance can run.

Some more info

Last edited by Aia; 01-16-2017 at 10:02 PM..
These 2 Users Gave Thanks to Aia For This Post:
# 4  
Old 01-16-2017
Interesting link, I usually don't need portability and use flock(1) for my own scripts.

The method using mkdir from that FAQ looks like it would be more reliable and still portable:

Code:
 lockdir=/tmp/myscript.lock
 if mkdir "$lockdir"
 then
     # Remove lockdir when the script finishes, or when it receives a signal
     trap 'rm -rf "$lockdir"' 0    # remove directory when script finishes

     # Optionally create temporary files in this directory, because
     # they will be removed automatically:
     tmpfile=$lockdir/filelist
 else
     echo "Script is currently in progress..aborting..."
     exit 3
 fi

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 01-17-2017
I use this block of code early in scripts that need to be run separately:-
Code:
# Set up / test lock file.  Hold lock for whole script
lockfile="/var/lock/my-lock-file"
[ ! -f $lockfile ] && touch $lockfile
exec 13>> $lockfile

flock -w 30 -x 13                                           # Do you feel locky?

if [ $? -ne 0 ]
then
   echo "$0 bypassed due to lock" >> $tracefile
   logger "$0 bypassed due to lock"
   exit 666
else
   echo "$0 has the the lock" >> $tracefile
   logger "$0 has the the lock"
fi

Naturally, I use a suitable file name for the lock file so that it is easy to recognise and re-use/avoid for other scripts. It seems to work for me. The tracefile variable and other things have already been set.

The -w flag on flock means that it will wait on file descriptor 13 for 30 seconds trying to get an exclusive lock (the -x flag) The file descriptor is left open by the >> redirector so the lock persists.

If the process is killed off ungracefully (i.e. a kill -9) then the file is then closed and the lock released, else it is closed when the script terminates gracefully or can be closed earlier if that is appropriate with exec 13>&- It negates the need for the trap ....... 0 which leaves you free to use it for something else if that is appropriate.


As they say, Tim Toady



Robin
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 01-17-2017
Yes flock(1) is the right way to do this.

The OP is after a portable solution, so lets test for executable /usr/bin/flock
and use that when available but revert to the mkdir method on systems without it:

Code:
if [ -x /usr/bin/flock ] || [ -x /bin/flock ]
then
    # If the env var $FLOCKER is not set,  then execute flock and
    # grab an exclusive non-blocking lock (using the script
    # itself as the lock file) before re-execing itself with the right
    # arguments.   It also sets the FLOCKER env var to the right value
    # so it doesn't run again.  Otherwise exit with status 3

    [ "${FLOCKER}" != "$0" ] && 
        exec env FLOCKER="$0" flock -E 3 -en  "$0"  "$0" "$@" || :
else
    # Here, even when two processes call mkdir at the same time, 
    # only one process can succeed at most. This atomicity of 
    # check-and-create is ensured at the operating 
    # system kernel level.

    lockdir=/tmp/myscript.lock
    if mkdir "$lockdir"
    then
        # Remove lockdir when the script finishes, or when it receives a signal

        trap 'rm -rf "$lockdir"' 0    # remove directory when script finishes
    else
        exit 3
    fi
fi

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 7  
Old 01-17-2017
Quote:
Originally Posted by Aia
All designs that try to check for a lock-file or pid-file are flawed. They are subjective to racecondition
The problem is that you can not check for the existence of a file and the creation of that file in an atomic action.
Actually - you can. The underlying filesystem calls support it (O_CREAT | O_EXCL), some shells have extensions for it, and for those who don't, there are ways to cheat a little - mkdir and mkfifo are atomic, they succeed and create a new filename or fail and don't, you can use them to "mutex" the creation of a PID file.

That a race condition may exist doesn't actually make ps | grep | awk | sed | cut | kitchen | sink | when | will | the | madness | end better than PID files, either. That's far from atomic, itself. It's also complicated, brittle, unreliable, and unportable on top of that.

There's a reason PID file is a de facto standard.

Last edited by Corona688; 01-17-2017 at 06:28 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To trigger script on particular instance

Hi Guys, I have a main_script.sh which runs every day and scheduled in crontab. in the main script i read data from config file test.config apple mango orange main_script.sh for i in `cat test.config` do if then echo 'Apple' (3 Replies)
Discussion started by: Master_Mind
3 Replies

2. Shell Programming and Scripting

Regarding a query on making changes to a running script

Hello All, Greetings !! I have a query here to all is as follows: Question: Let's say we are running a script in a UNIX box and we have opened an another session and then made changes in script of some statements NOT to be print some values(just an example) so when I am monitoring the... (5 Replies)
Discussion started by: RavinderSingh13
5 Replies

3. UNIX for Advanced & Expert Users

How to check a single process instance is always running?

Hi, I want to write one program in C in Unix OS which will check the running status of a process time to time. If the process is stopped somehow by any means, it will ensure that the process is restarted and only one copy of the process image should run in memory at any point of time for the user.... (2 Replies)
Discussion started by: sanzee007
2 Replies

4. Shell Programming and Scripting

Process Instance not running properly.

I have run 10 instances of the process eg, process name is BG nohup /WP01IRB1_irbapp/IRBWPROD/RB/bin/BG -c 1 -t 23 -a '-caTop TESTBILLCYCLE='5FEB13_81PT19NPT''>a.txt & nohup /WP01IRB1_irbapp/IRBWPROD/RB/bin/BG -c 2 -t 23 -a '-caTop TESTBILLCYCLE='5FEB13_81PT19NPT''>b.txt & nohup... (3 Replies)
Discussion started by: ankitknit
3 Replies

5. Shell Programming and Scripting

Making use of multiple cores for running sed and awk scripts

Hi All, After reading that the sort command in Linux can be made to use many processor cores just by using a simple script which I found on the internet, I was wondering if I can use similar techniques for programs like the awk and sed? #!/bin/bash # Usage: psort filename <chunksize>... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

6. Shell Programming and Scripting

Print available running instance out of total

Hello. I have a status command in AIX box, which provides output as below: $ status You are running the application on pegasus2 ----Program Name------|--Avail / Total---------| MQ | 1/2 | ORACLE | 10/10 | TMADMIN ... (3 Replies)
Discussion started by: panchpan
3 Replies

7. Shell Programming and Scripting

How to restrict running one instance of scp at any time in fsniper

How to restrict running one instance of scp at any time? (2 Replies)
Discussion started by: proactiveaditya
2 Replies

8. Shell Programming and Scripting

[PHP] endless loop mimics a cron. Make sure only one instance is running

Hi, PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime. I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file. However, I'd like to make sure... (2 Replies)
Discussion started by: jjshell
2 Replies

9. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

10. Shell Programming and Scripting

How to check if another instance of the process is running

Hi, I am writing a shell script to invoke a C++ program. Before I start the C++ program (oi7loadbalancer), I am checking if the process is already running. I start the process only if it is not already running. I have the following check in my script. proccount=`ps -f -u $USER_NAME | grep... (8 Replies)
Discussion started by: sim
8 Replies
Login or Register to Ask a Question