How to check if a shell is already running ?


 
Thread Tools Search this Thread
Operating Systems Solaris How to check if a shell is already running ?
# 1  
Old 02-12-2012
How to check if a shell is already running ?

Hi,
I put this at start of my shell (korn shell) to be sure that the shell is not already running and sometimes it fails and says that it is already running which is not true !

Code:
sleep 1
/usr/bin/ps -ef | /usr/bin/grep "$0" | /usr/bin/egrep -v grep>$LOGDIR/$0.res
isup=$(cat $LOGDIR/$0.res|wc -l)
if [[ $isup -gt 1 ]]
then
print "$0 is already running - Aborting"
rm $LOGDIR/$0.res
exit 0
fi

Does someone knows a better way to do this ?

Last edited by DukeNuke2; 02-12-2012 at 07:25 AM..
# 2  
Old 02-12-2012
What exactly are you trying to achieve here? Ensure that you are not in a nested shell, i.e. that SHLVL is 1?
# 3  
Old 02-12-2012
I assume you have a long running script and are wanting to prevent a second copy from starting if the first is running. What is probably happening is that your test is seeing 'itself' in the output from ps and is claiming that another copy is already running.


When I have to ensure that the invocation of a script is the only one running I use a marker directory in /tmp. Only one process can create a directory, so if two are started at the same time then only one will 'win' and continue running. Further, I add some information to the directory so that when another detects one already running, it can verify that it is still running and allows itself to start if it finds a marker, but doesn't find an associated process.


Here's an example:
Code:
#!/usr/bin/env ksh

trap "rm -fr \${solo_mark:-nosuchfile}" EXIT            # remove our marker stuff at exit

cmd=${0##*/}
solo_mark=/tmp/$cmd.solo
if ! mkdir $solo_mark 2>/dev/null       # if two started at the same time only one will be successful
then
    sleep 1                             # if there was a collision, allow winner to set the info file
    read rinfo <$solo_mark/info
    echo "verification that '$rinfo' is still running"
    if ps -e -o pid,command |awk -v rinfo="$rinfo" '
        BEGIN { split( rinfo, a, " " ); }
        $1 == a[1] {
            if( rinfo == $0 )
                exit( 1 );
            next;
          }'
    then
        echo "marker exists, but not running; we continue"
    else
        echo "already running"
        exit 1;
    fi
else
    echo "not running"
fi
ps -o pid,command | grep "^[ ]*$$" >$solo_mark/info     # save our info for others to verify
 


# ------- do intended work here -----------------

The bit of extra work to save the info, and verify it, makes things easier following a system crash, or someone killing the process in such a way that the marker doesn't get removed.


Hope this helps.

Last edited by agama; 02-12-2012 at 12:39 PM.. Reason: formatting
# 4  
Old 02-13-2012
Thanks for the help, but as you said, the problem with the 'test&set' method is the reinit after system crash or manual killing.
My problem is that I see 2 lines, the shell process id and it's parent, I must be able to make the difference between the parent and an other process.
What I dont understand also is that i do it on 2 different machines and I get 2 differents outputs.
On the 1rst machine (spark v210) I get
root 14550 14543 0 10:40:48 pts/2 0:00 /usr/bin/ksh test.sh
root 14543 14016 0 10:40:45 pts/2 0:00 /usr/bin/ksh test.sh
As you can see these 2 lines are in fact related to the same process, the first line is in fact the parent process of the process appearing in the 2nd line.
the real solution is to store the PID and the PPID in 2 different arrays and build an algorythm that will be able to understand this situation.
On the second machine (spark v120) i get a single line.
I think that it is because that the first one got 2 processors and the 2nd one a single processor.

---------- Post updated at 12:48 PM ---------- Previous update was at 11:24 AM ----------

I think I got something :
here is how to manage the 'Parent process' case

Code:
set -A pids Dum
set -A fpids Dum
ps -ef| grep $0 | grep -v grep > $LOGDIR/$0.pid

i=0
j=0
while read filename
do
  pids[i]=$(echo $filename | awk '{printf "%s ",$2}') # I store the pid
  fpids[i]=$(echo $filename | awk '{printf "%s ",$3}') # I store the parent pid
#  echo "pids fpids index $i =" ${pids[i]} ${fpids[i]}

  (( i=i+1 ))
done < $LOGDIR/$0.pid

while [[ $j -lt $i ]]
do
if [[ ${pids[$j]} -ne $$ ]] # if the considered pid is not me then
if [[ ${fpids[$j]} -ne $$ ]] # and even not my parent then
print " $0 is already running. Exiting" #well it is a stranger exit 0
fi
fi (( j=j+1 ))
done


Last edited by DukeNuke2; 02-13-2012 at 08:13 AM..
# 5  
Old 02-13-2012
Quote:
Originally Posted by zionassedo
Thanks for the help, but as you said, the problem with the 'test&set' method is the reinit after system crash or manual killing.
Actually, I said that the bit of extra work was to handle that situation so that it wasn't a problem. With the example I posted restart is clean following a crash or kill without any manual effort to clean up.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check status of long running multiple curl commands in shell script

I am working on script. it reads a file which contains multiple lines Ex; curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=1 curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=2 curl --write-out %{http_code} --silent ... (2 Replies)
Discussion started by: oraclermanpt
2 Replies

2. Shell Programming and Scripting

Help with Check Syntax of Shell Script without Running

Hi Everyone, Is there any way ( generic) to check syntax of Shell Scripts without running it?? (4 Replies)
Discussion started by: roy121
4 Replies

3. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

4. Shell Programming and Scripting

need to check if the process is running

Hi, I check if the process is running or not using the below. /usr/ucb/ps auxww | grep 109 |grep rmi | awk '{print $2}' 9718 Thus we see 9718 is the PID. It return blank if the process is not running. I need to perform some action if the process is not running and leave it if... (8 Replies)
Discussion started by: shifahim
8 Replies

5. Shell Programming and Scripting

Check what cron is running

How to check what cron is scheduled to run? I don't want to modify anything. Thanks (1 Reply)
Discussion started by: stevensw
1 Replies

6. Programming

How do I check if a process is running in C

How to I check if a process is running in C? I'm trying to use ps aux |grep "process name" but failing in doing that. How do I do that? Thanks, (1 Reply)
Discussion started by: cprogdude
1 Replies

7. UNIX for Advanced & Expert Users

how to check if a process is running in a server from shell script.

I want to write a unix shell script that will check if a process (say debu) is running in the server or not. If no , then send a mail to the corresponding person to start the process??? (2 Replies)
Discussion started by: debu
2 Replies

8. Shell Programming and Scripting

How to check for specific process running through shell

Hi I want to check whether specific process is running or not through a shell script. ps -ef | grep will tell me this, i tried this way in shell cnt =`ps -ef| grep xyz | awk '{ END {print NR}}` if it return two rows, job is running else not. Is there any better way of doing this. (3 Replies)
Discussion started by: agp
3 Replies

9. Shell Programming and Scripting

check that script is not running twice

using ps -ef | fgrep "ld_data" how do i write a script to check that it didn't already run Thanks (2 Replies)
Discussion started by: Link_02
2 Replies

10. UNIX for Dummies Questions & Answers

check my code?? running shell from c?

Hi mates, i am trying to run a shell command from my C program in this case let is say the "ls" command. It copiles it okay, and even creates the new fork too. But seems to nothing is happening i mean it is not showing the result of the "ls" command. I don't know wat i am doing wrong. Any... (1 Reply)
Discussion started by: abdul
1 Replies
Login or Register to Ask a Question