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)
What I am looking for is some way to determine if the script is running as in function 1.
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.
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):
This User Gave Thanks to Don Cragun For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)