1753379 Members
5543 Online
108792 Solutions
New Discussion юеВ

Re: trap process id

 
SOLVED
Go to solution
Dominic Ouellet
New Member

trap process id

I everybody,

Here is the situation. I need to recover the PID of a program I launched in background (from a script). Other scripts may have launched the same program so a simple ps | grep command wouldn't do the job. I feel unsure with the $! command (witch return the PID of the last background program that has been launched) because two script could launch background programs at the same time and then, I can't be sure I got the PID of my program. I also heard about the ps -H but it only applies for XPG4. Any ideas?

Thanks for your help...
Dom
4 REPLIES 4
Dan Bonham
Advisor
Solution

Re: trap process id

Dom,
I think the $! is the best way to go. You should be ok because $! is local to the shell you are in so it should not know the PID of a background process started in another shell.

Rusty
James R. Ferguson
Acclaimed Contributor

Re: trap process id

Hi:

While pid values will eventually be reused (when they are no longer in use), there is no problem with capturing a pid in a variable and later referencing/testing it.

See this recent thread for some examples:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0xfe7387dc4d7dd5118ff00090279cd0f9,00.html

ALSO: if you want to leverage the UNIX95 options of 'ps' all you need do is arm the UNIX95 variable for the duration of the 'ps' like this (your offering):

UNIX95= ps -H

Note the blank character after the equal sign and the absence of any semicolon! This syntax sets UNIX95 only for the command on the line it is written.

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: trap process id

Hi,

It doesn't matter if another process also spawns a process of the same name. $! will definitely be the pid of the last background child process spawned by this shell.

Clay
If it ain't broke, I can fix that.
Dominic Ouellet
New Member

Re: trap process id

Thanks to both of you guys
I'm now a happy camper :-)