AIXThis forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am looking for a piece of logic that I can add to a shell script which will avoid it to run more than 1 instances simulataneously at any given time.
This is my rough plan :-
Add logic to an existing script such that at start up it performs a check to see if an instance of itself is already running on the server.
If there is already an instance running, log a comment in the LOG file and end without processing this 2nd instance.
Please help me with your solutions at the earliest.
The green part checks to see if a lock file is present. I'm not interested to see the results, that's why the bold part is there (it redirects all output to /dev/null). The exit code of the ls command is used (0 if a file is found or not zero if the file isn't found).
The brown part is special: The part on the right of the && is only executed when a file is found (the expression is true). If no file is found the expression is false and all after && is not executed. There's also a way to check for false (|| instead of &&).
This is a short form of the following construct:
Code:
ls /tmp/scriptname.lock.* 1>/dev/null 2>&1
exitCode="$?"
if [[ "$exitCode" == "0" ]]
then
echo "Already running" >> /path/to/log_file
exit 1
fi
Yes, I was initially thinking of using a ps -ef | grep "script_name" to identify whether an instance is already running, but I don't know how to stop the script from running another time after identifying it using ps.
Do you have a suggestion on this kind of solution ?
Druuna's solution looks comprehensive though. Will test it tomorrow as I don't have access to the AIX server now.
As mentioned earlier, I am new to shell scripting. It will be great if you can share the code of your logic so that I can understand your solution better.
Key concepts:
* Create a lock file (similar to druuna's original code above)
* Save the script's process ID in the lock file
* If the file exists, use "ps" to check whether it is actually still running. This handles cases where the script might have terminated abnormally (think power failure while runnning)
* Use "trap" to handle some causes for premature termination (hangup/interrupt/quit signals)
The code:
Code:
#!/usr/bin/ksh
# Sample script for locking a shell process
BASE=$(basename $0 .sh)
LOCK=/tmp/${BASE}.lock
date
# Check for lock file
if [ -f $LOCK ]
then
PID=$(cat $LOCK)
echo "Lock file found for $0 - pid $PID"
ps -fp $PID
if [ $? -eq 0 ]
then
echo "$BASE is locked by pid $PID - not executing $0 now."
exit 0
else
echo "Previous lock file appears to be stale - ignoring"
fi
fi
# Create lock file
echo $$ > $LOCK
trap "rm ${LOCK}" 1 2 3
date
# now do something useful
banner hello
sleep 10
date
# Go away
rm $LOCK
exit 0
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.