cancel
Showing results for 
Search instead for 
Did you mean: 

Killing parent pid

 
SOLVED
Go to solution
Highlighted
Srinikalyan
Regular Advisor

Killing parent pid

Will killing the parent pid kills all its child process?
user1 3870 1667 1 Sep 9 ? 4:52 b
user1 3871 1667 0 Sep 9 ? 4:50 c
user1 3878 1667 0 Sep 9 ? 4:49 d
user1 1667 1 0 Sep 9 ? 12:33 a
user1 3868 1667 0 Sep 9 ? 4:44 e

a is the parent process and b,c,d,e are the child processes.

Thanks,
Srini
12 REPLIES 12
Mel Burslan
Honored Contributor
Solution

Re: Killing parent pid

It depends. If the software was written in a way to clean up properly, yes, the child processes should be killed when the parent process dies. But the software might be written deliberately (or sometimes accidentally or even in a sloppy way) not to do this, i.e., the child processes live after the parent dies being owned by a PPID of 1.

It all depends on the software and there is no definite answer as I know of.
________________________________
UNIX because I majored in cryptology...
Srinikalyan
Regular Advisor

Re: Killing parent pid

Is there any script to find all the child processes if we give the input as the parent pid?
Mel Burslan
Honored Contributor

Re: Killing parent pid

simple..

PPID=whatever
ps -ef | awk {'print $3'} | grep $PPID
________________________________
UNIX because I majored in cryptology...
Mel Burslan
Honored Contributor

Re: Killing parent pid

sorry the command I gave will not produce any output you deserve but this should:

PPID=whatever
ps -ef | grep $PPID

this output will also include a line for the parent process as well as all the child processes
________________________________
UNIX because I majored in cryptology...
Bill Hassell
Honored Contributor

Re: Killing parent pid

> Is there any script to find all the child processes if we give the input as the parent pid?

The ps command can show the entire hierarchy:

UNIX95=1 ps -eH
or
UNIX95=1 ps -efH

Be sure to copy/paste the entire line. UNIX95=1 sets a special option for ps.

But the best tool is pstree. Here's a link to it: http://en.wikipedia.org/wiki/Pstree

pstree -p #####

where ##### is any process in the tree. pstree will show the entire branch up and down.


Bill Hassell, sysadmin
Roland Piette
Regular Advisor

Re: Killing parent pid

Hello Bill,

In HP-UX 11.31 ptree exists and do the same as pstree.

Regards,
Roland
Dennis Handly
Acclaimed Contributor

Re: Killing parent pid

>Mel: PPID=whatever; ps -ef | grep $PPID

A safer command would be:
PPID=whatever
UNIX_95=EXTENDED_PS ps -ef -opid= -ocomm= -oppid= | grep " $PPID$"

This makes sure you can search for PPID=1.
Srinikalyan
Regular Advisor

Re: Killing parent pid

I finally wrote script like below. The script will first kill all the child of the ppid and then finally ppid itself.

Script starts here:
ppid=`ps -ef|grep 'TWM -u'|grep -v grep|awk '{print $2}'`
if [ -z "$ppid" ]
then
echo "no parent TM process running"
else
echo " Child processes are "
for child in `ps -ef|grep -w $ppid|grep -v grep|awk '{if($3!=1) {print $2}}'`
do
echo $child
#replace echo with kill
done
echo "Parent TM is " $ppid

fi

Thanks,
Srini



Bill Hassell
Honored Contributor

Re: Killing parent pid

Warning: grep and ps will almost always give you wrong answers!!!

Always use ps to find processes by name, never grep. In your script, grep "TWM -u" may be unique but you can't depend on that. If you want to find all the TWM processes, use ps like this:

UNIX95=1 ps -C TWM -o pid= -o args=

This method ensures that grep will not find something else (like grep). Now if several copies of TWM are running and you want to select only the one with -u, then you can add grep to the result like this:

UNIX95=1 ps -C TWM -o pid= -o args= | grep -- -u

Be sure to use two dashes (grep --) to terminate the option processing and treat "-u" as a simple string.

But most important, do NOT grep for pid or ppid!!! This will find critical processes that happen to have matching numbers in the run string. ALWAYS use ps -p for exact PID matches.


Bill Hassell, sysadmin