1833777 Members
1950 Online
110063 Solutions
New Discussion

Trap the process id

 
SOLVED
Go to solution
Patrick Chim
Trusted Contributor

Trap the process id

Hi all,

For example, if I run a script that will bring up the application, how can I do to capture all the processes id of the daemons which is brought up by that script ?

Many thanks,
Patrick
7 REPLIES 7
Stefan Farrelly
Honored Contributor

Re: Trap the process id


A properly written deamon will parent itself to PID=1 so thats not going to help you. But check anyway, with luck they will have a PPID (parent pid number) matching the process you used to start them.

Other ways to trap them are to a date command before you start the deamons, then another at the end, and note the times, then do a ps and check the start times of all processes (STIME) belonging to that userid, and you will see those that sarted with the timeframe of your date commands.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Carlos Fernandez Riera
Honored Contributor

Re: Trap the process id

If you try to get PIDs you run in background:

/tmp/prg1 &
PRG1_PID=$!

unsupported
Andrew Cowan
Honored Contributor

Re: Trap the process id

Hi,

A quick-and-dirty method is just to call "grep" and then "awk" out the PID. E.g:

ps -ef | grep mydeamon | grep -v grep | awk '{print $2}'

Its not beautiful, but it does the trick. Won't solve the PPID of 1 problem though. Some deamons also create ".PID" files when they are run.
Patrick Chim
Trusted Contributor

Re: Trap the process id

Hi all,

Let me take an example and state out the question clearly.
If I bring up Informix database by command oninit, it will fork several oninit processes as well. Can I capture the process id of those child processes.

informix 5182 1 28 16:49:18 ? 0:00 oninit
informix 5183 5182 0 16:49:18 ? 0:00 oninit
informix 5184 5182 76 16:49:18 ? 0:01 oninit
informix 5185 5184 0 16:49:18 ? 0:00 oninit
informix 5186 5184 0 16:49:19 ? 0:00 oninit
informix 5187 5182 0 16:49:20 ? 0:00 oninit
informix 5188 5184 0 16:49:21 ? 0:00 oninit
informix 5189 5184 0 16:49:22 ? 0:00 oninit

In this example, how can I get the process id 5182 to 5189.

Many thanks,
Patrick
Carlos Fernandez Riera
Honored Contributor
Solution

Re: Trap the process id


Perhaps informix set process group, Try :

ps -fg 5182

See man ps.
unsupported
Patrick Chim
Trusted Contributor

Re: Trap the process id

Carlos,

It works fine. But how can I know the pid is 5182. Can I grep this pid after I issue the command ?

Thanks,
Patrick
Carlos Fernandez Riera
Honored Contributor

Re: Trap the process id

MASTER=$(ps -aef | grep oninit | awk ' $3 == 1 { print $2; exit}')

ps -fg $MASTER

?????
unsupported