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 !
Does someone knows a better way to do this ?
Last edited by DukeNuke2; 02-12-2012 at 07:25 AM..
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:
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
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
Last edited by DukeNuke2; 02-13-2012 at 08:13 AM..
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.
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)
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)
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)
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)
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)
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)
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)