Script function which checks if itself is already running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script function which checks if itself is already running
# 1  
Old 06-16-2013
Script function which checks if itself is already running

Hi All,

I have a cron job set up which is set to run every 10 seconds.
What I need to do is have the script do a check to see if it is already running such that if it is running it wont fire up additional instances and processes according to its normal process.

For example if I have a script which checks to see if a file exists, and if exists, does a copy or move function of the file to another location, and then runs another process on it. If it is a large file that takes a long time to copy, and/or the other processes which I run on it takes a long time to complete, I don't want it to automatically fire up another instance of itself every 10 seconds.

So, lets say I have a script like the following: (and this isn't proper code, but just to illustrate the functions)

Code:
#!/bin/bash
#

1) Check to see if I am already running, and if so exit.

2) Check to see if file exists, and if it does process it.

EOF

What I am looking for is some way to determine if the script is running as in function 1.

Does anyone have any ideas how to achive this?

thanks,
landos
# 2  
Old 06-16-2013
Create "lock file" when starting the script and remove it after script is done, for example:
Code:
if [ -f /var/tmp/script.lock ]; then
  echo "Already running. Exiting."
  exit 1
else
  touch /var/tmp/script.lock
fi

<main script code>

rm /var/tmp/script.lock

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-16-2013
Rather than use /var/tmp, the more common place to put lock files is in /var/run.

You also need a trap statement to remove the lock if somebody or something terminates the script.
Code:
LOCKFILE=/var/run/script.lock

trap "{ rm -f $LOCKFILE; exit 255; }" EXIT

if  [ -f $LOCKFILE ]
then
   echo "Already running. Exiting."
   exit 1
fi

touch $LOCKFILE

<main script code>

exit 0

This User Gave Thanks to fpmurphy For This Post:
# 4  
Old 06-16-2013
Ah, thanks..
The trap function was going to be my next question.
# 5  
Old 06-18-2013
Quote:
Originally Posted by fpmurphy
Rather than use /var/tmp, the more common place to put lock files is in /var/run.

You also need a trap statement to remove the lock if somebody or something terminates the script.
Code:
LOCKFILE=/var/run/script.lock

trap "{ rm -f $LOCKFILE; exit 255; }" EXIT

if  [ -f $LOCKFILE ]
then
   echo "Already running. Exiting."
   exit 1
fi

touch $LOCKFILE

<main script code>

exit 0

There are a couple of problems here. First, if the script is already running, the trap removes the lock file after reporting that the script is running.

And second, since the test for the lock file and the creation of the lock file is not a single atomic operation, there is a chance that the test by two are more invocations of this script could all succeed without knowing that another script had also succeeded.

The following script moves the trap on EXIT so that it only removes the lock file after this invocation of the script successfully creates the lock file. It also uses set -C to make the test for existence of the lock file and the creation of the lock file a single atomic operation. It also stores the PID of the lock holder in the lock file.

Unlike fpmurphy's script, this script does not remove the lock file if the script is terminated by a common signal. Note that the SIGKILL signal can't be caught and the lock file will never be removed in this case. If the script is terminated by a trapped signal, the exit code will be 128 plus the signal number; and if the script terminated normally, the exit code will be 0. (The script fpmurphy provided always exits with exit code 255 whether or not the script was terminated by a signal or completed successfully.) I did not install a trap on SIGALRM because this script uses sleep for demonstration purposes and some implementations of sleep use the SIGALRM signal. If you choose to catch other signals, you can find the signal number for them in /usr/include/signal.h (or something it #includes); the numbers for the signals used in this sample are supposed to be the same on all implementations that conform to the POSIX standards and the Single UNIX Specifications. If you want to remove the lock file in some or all of the signal catching cases, remove the trap "" EXIT from the trap for the desired signal(s):
Code:
#!/bin/ksh
IAm="${0##*/}"
LF="/var/run/$IAm.lock"

set -C
if  ! echo $$ > "$LF"
then
  printf "%s: Exitting, Already running as pid %s.\n" "$IAm" "$(cat "$LF")" >&2
  exit 1
fi
set +C

trap 'rm -f "$LF"' EXIT
trap 'echo "$IAm: Terminated by ABRT signal; $LF not removed." >&2
  trap "" EXIT
  exit 134' ABRT
trap 'echo "$IAm: Terminated by HUP signal; $LF not removed." >&2
  trap "" EXIT
  exit 129' HUP
trap 'echo "$IAm: Terminated by INT signal; $LF not removed." >&2
  trap "" EXIT
  exit 130' INT
trap 'echo "$IAm: Terminated by QUIT signal; $LF not removed." >&2
  trap "" EXIT
  exit 131' QUIT
trap 'echo "$IAm: Terminated by TERM signal; $LF not removed." >&2
  trap "" EXIT
  exit 143' TERM

echo 'other processing goes here; echo and sleep for demo'
echo "pid $$ sleeping"
sleep 300

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with delaying script and implementing checks before completion

Hello everyone, I was wondering if any of you could help me with this. I am an absolute beginner and don't know how to program, but I can follow a tutorial and tweak code sometimes. My understanding of programing is limitted to what for and while loops do, and how if then else logic works. That... (2 Replies)
Discussion started by: tomeurp
2 Replies

2. Shell Programming and Scripting

Script to do the following checks

Hi , I need a script for processing below scenario. I have to check daily by doing ftp IP to check it is logging or not. So i want this activity to be automated such that if login succesful i will get "FTP LOGIN SUCCESS" in a log file and if fails i want the error message in the same log... (1 Reply)
Discussion started by: sv0081493
1 Replies

3. Shell Programming and Scripting

Script to performs checks

Hi , I need a script which performs below activity I have one file named "testfile" in 9 different directories with same name. I want to perform below action with each testfile of each directory. if ; then mv listfiles listfiles_`date +%b%y` else echo No Such files fi ... (4 Replies)
Discussion started by: sv0081493
4 Replies

4. Shell Programming and Scripting

Function not running in script.

I an using the below functions in my script. --------- checkRT() { subHeader "Runtime Check" for nn in `cat $HOST_FILE|egrep -i 'msdp|ca|backup|db'|grep -v '#'|sed '/^$/d'|awk '{ print $1":"$2":"$3}'` do fhba=`expr $nn|cut -d: -f2` fhba2=`expr $nn|cut -d: -f3` subSubHeader $fhba2 ssh... (1 Reply)
Discussion started by: nandan8a
1 Replies

5. Shell Programming and Scripting

[Bash] MD5 Checks with Script.

Hi. I'm triyng to make a Bash Script that checks (recursively) the MD5 from all the files in a certain directory and compare them against some other check that should be already done and saved in a file. I've reached to the point where i have the MD5 from the file and the MD5 that the script... (1 Reply)
Discussion started by: BiFo
1 Replies

6. Shell Programming and Scripting

Running a function on a remote server via SSH in a script

I'm working on a script (mostly for practice) to simplify a task I have to do every now and then. I have a cluster with 6 servers on it, each server has a directory with a set of files called *.pid and *.mpid. Each file contains the pid of a process that may or may not be running on that server.... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

7. Shell Programming and Scripting

Script stops running the remaining checks after becoming admin

Hi all, I encountered a problem where my script stops running the remaining checks after becoming an admin that is written within the script. For example: ========================================= #!/bin/sh check 1 # Runs successfully check 2 # Runs successfully /com/bin/admin #... (1 Reply)
Discussion started by: seanchew
1 Replies

8. Shell Programming and Scripting

Running function or command concurrently in a bash script

I currently run a script over a vpnc tunnel to back-up my data to a remote server. However for a number of reasons the tunnel often collapses. If I manually restore the tunnel then the whole thing can continue, but I want to add into my script a section whereby while the transfer is taking place,... (8 Replies)
Discussion started by: dj_bridges
8 Replies

9. Shell Programming and Scripting

Want to make a script that checks connection protocol

Hello all, I currently connect to several servers multiple times a day. Most of the time I connect via SSH through the terminal emulator poderosa (my personal favorite), but sometimes I connect via telnet through xstart because I need it to export a GUI. What I want to do is add something to... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

10. Shell Programming and Scripting

Script that checks for previous instances running

Hello, I'm trying to write a script that checks for previous instances of the same script which may still be running (this script is scheduled to run every 30 minutes). I want to somehow use the pid from each instance to make sure the previous one isn't running before continuing with my... (5 Replies)
Discussion started by: bd_joy
5 Replies
Login or Register to Ask a Question