LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices

Reply
 
Search this Thread
Old 03-06-2013, 05:07 AM   #1
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 26

Rep: Reputation: Disabled
Arrow Logic to avoid duplicate instances


Hi,

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.

Thanks in advance !

Last edited by arjundey; 03-06-2013 at 05:08 AM.
 
Old 03-06-2013, 05:18 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Are you looking for something like this:
Code:
#!/bin/bash

# is previous run still active
ls /tmp/scriptname.lock.* 1>/dev/null 2>&1  && echo "Already running" >> /path/to/log_file && exit 1

# set lock file
touch /tmp/scriptname.lock.$$

# --------------------------------------------------------------------------- #
# rest of the code goes here 

.
.

# --------------------------------------------------------------------------- #

# cleanup
# remove lock
rm -f touch /tmp/scriptname.lock.$$
exit 0
There are probably other ways of doing this as well.

Last edited by druuna; 03-06-2013 at 05:20 AM. Reason: fixed a typo (goed -> goes)
 
Old 03-06-2013, 09:00 AM   #3
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 26

Original Poster
Rep: Reputation: Disabled
Hey Druuna,

Thanks for your reply.

I am really new to shell scripting and therefore, I request you to explain what this command will do or how this logic works ? :

ls /tmp/scriptname.lock.* 1>/dev/null 2>&1 && echo "Already running" >> /path/to/log_file && exit 1
 
Old 03-06-2013, 09:27 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Quote:
Originally Posted by arjundey View Post
I am really new to shell scripting and therefore, I request you to explain what this command will do or how this logic works ? :

ls /tmp/scriptname.lock.* 1>/dev/null 2>&1 && echo "Already running" >> /path/to/log_file && exit 1
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
Also have a look here: Resources / Useful Links (especially the Bash links).
 
Old 03-06-2013, 09:30 AM   #5
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 26

Original Poster
Rep: Reputation: Disabled
I am clear now. Thanks !
 
Old 03-06-2013, 10:55 AM   #6
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,758

Rep: Reputation: Disabled
Quote:
Originally Posted by arjundey View Post
I am clear now. Thanks !
Can you write a script based on output of ps -A ?
 
Old 03-06-2013, 11:09 AM   #7
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 26

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-06-2013, 11:28 AM   #8
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,758

Rep: Reputation: Disabled
1. If not running then restart would be the way with ps.
2. use flock (improves use of lock files)
3. deamon
 
Old 03-06-2013, 11:36 AM   #9
arjundey
LQ Newbie
 
Registered: Sep 2011
Posts: 26

Original Poster
Rep: Reputation: Disabled
Hey Mani,

Thanks for your reply.

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.

Thanks !
 
Old 03-07-2013, 01:34 AM   #10
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 267

Rep: Reputation: 112Reputation: 112
Hi guys,

For what it's worth, here's my approach to this.

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
Regards,

Clifford
 
Old 03-12-2013, 06:40 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,713

Rep: Reputation: 488Reputation: 488Reputation: 488Reputation: 488Reputation: 488
You seem to be reinventing pidfile.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sendmail-2: Wrong number of instances of process sendmail:, expected instances equal maxymaxymaxymaxymaxy Linux - Newbie 1 06-15-2011 10:51 AM
does tar or bzip2 squash duplicate or near-duplicate files? garydale Linux - Software 6 11-19-2009 04:43 PM
LSI Logic / Symbios Logic 53c875 (rev 14) -> HP Storageworks 1/8 G2 gileravxr Linux - Hardware 0 07-21-2009 04:45 AM
Logic Problem lucky6969b Programming 9 12-18-2005 10:44 PM
logic h/w Programming 8 01-02-2004 04:44 PM


All times are GMT -5. The time now is 12:13 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration