Sponsored Content
Operating Systems Solaris How to check if a shell is already running ? Post 302597988 by zionassedo on Monday 13th of February 2012 05:48:55 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PIDOF(8)						Linux System Administrator's Manual						  PIDOF(8)

NAME
pidof -- find the process ID of a running program. SYNOPSIS
pidof [-s] [-c] [-n] [-x] [-o omitpid[,omitpid..]] [-o omitpid[,omitpid..]..] program [program..] DESCRIPTION
Pidof finds the process id's (pids) of the named programs. It prints those id's on the standard output. This program is on some systems used in run-level change scripts, especially when the system has a System-V like rc structure. In that case these scripts are located in /etc/rc?.d, where ? is the runlevel. If the system has a start-stop-daemon (8) program that should be used instead. OPTIONS
-s Single shot - this instructs the program to only return one pid. -c Only return process ids that are running with the same root directory. This option is ignored for non-root users, as they will be unable to check the current root directory of processes they do not own. -n Avoid stat(2) system function call on all binaries which are located on network based file systems like NFS. Instead of using this option the the variable PIDOF_NETFS may be set and exported. -x Scripts too - this causes the program to also return process id's of shells running the named scripts. -o omitpid Tells pidof to omit processes with that process id. The special pid %PPID can be used to name the parent process of the pidof pro- gram, in other words the calling shell or shell script. EXIT STATUS
0 At least one program was found with the requested name. 1 No program was found with the requested name. NOTES
pidof is actually the same program as killall5; the program behaves according to the name under which it is called. When pidof is invoked with a full pathname to the program it should find the pid of, it is reasonably safe. Otherwise it is possible that it returns pids of running programs that happen to have the same name as the program you're after but are actually other programs. Note that that the executable name of running processes is calculated with readlink(2), so symbolic links to executables will also match. SEE ALSO
shutdown(8), init(8), halt(8), reboot(8), killall5(8) AUTHOR
Miquel van Smoorenburg, miquels@cistron.nl 01 Sep 1998 PIDOF(8)
All times are GMT -4. The time now is 08:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy