1833059 Members
2698 Online
110049 Solutions
New Discussion

Find PID's

 
Vibhor Kumar Agarwal
Esteemed Contributor

Find PID's

I have a script which spawns multiple compilations.

Sometimes due to some errors i have to terminate that script.
Now comes the main part, how do I do that?

I can see the individual compilations via
ps -ef | grep compiler
and also kill them via
kill -9 pid

But the scirpt continues:

How do i find out which is the master script which gave birth to all these sub-processes.
This can't be a single step of seeing the PPID in the ps, as its a 3-4 step parent-child process.

Thanks.
Vibhor Kumar Agarwal
9 REPLIES 9
Simon Hargrave
Honored Contributor

Re: Find PID's

Depending how your child processes are called, you can use "ps -g PID". Assuming PID is the process group leader.

For example if I have a shell and run a long find command, then from another shell I do ps -g I get: -

$ ps -g 4068
PID TTY TIME COMMAND
4068 pts/17 0:00 sh
12200 pts/17 0:00 find

If this doesn't work for you, and you have control over the scripts that run your compiles, you could have them all output their PID to a file in /var/run, and then you can kill the entire list if required.
Peter Godron
Honored Contributor

Re: Find PID's

You have to track back via the PPID of one of the compile processes.
Please remember to cancel the child before the parent!
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Find PID's

Thanks Simon and Peter.

But i can't backtrack as the tree goes to 4-5 steps.
Further by the time i start traversing the tree some of the child process completes giving me an illegal PID.
Vibhor Kumar Agarwal
Peter Godron
Honored Contributor

Re: Find PID's

Dump the output of the ps -ef command to a file to geta frozen status, then use that file to trace the PID and PPID.
Example:

Process x has ppid 1234
find process with pid 1234, which will give you PPID 2345
repeat until PPID is 1
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Find PID's

If i have to do this, then i will say let the script do whatever it wants to do and i will start all over again.
Vibhor Kumar Agarwal
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Find PID's

Simon,

How do i get the PID of the process group leader.

This is what i am trying to find out.
Vibhor Kumar Agarwal
Simon Hargrave
Honored Contributor

Re: Find PID's

How you determine this will depend on how you run your script. If it runs from cron you can check the cron log for the PID. Otherwise ps and grep for "compile_script.sh" or whatever is the "master" script, and this will be your group leader.
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Find PID's

No,

We simply run it from the command prompt.
I thought of giving the name of script and grep but it won't work as sometimes we don't know the name of script as the running person is a different one.

Don't tell me to ask that person the script name.
Vibhor Kumar Agarwal
Ermin Borovac
Honored Contributor

Re: Find PID's

Given a process ID (e.g. your compiler) you can find process group ID with

$ UNIX95= ps -p -o pgid=

Then you can find PIDs of all processes assocated with this session with

$ UNIX95= ps -o pid= -g

To put it all together (untested)

$ kill -2 $(UNIX95= ps -o pid= -g $(UNIX95= ps -p -o pgid))