cancel
Showing results for 
Search instead for 
Did you mean: 

Killing parent pid

 
SOLVED
Go to solution
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
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

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
James R. Ferguson
Acclaimed Contributor

Re: Killing parent pid

Hi Srini:

> ...replace echo with kill

And while we are on the subject of killing processes, don't use 'kill -9' without first attempting a simple 'kill' first.

A 'kill -9' cannot be caught and thus a process has no (programatic) chance to cleanup temporary files and/or shared memory segments.

Instead, escalate your annihilation:

kill -TERM ${mypid} > /dev/null 2>&1
sleep 2
kill -HUP ${mypid} > /dev/null 2>&1
sleep 2
kill -9 ${mypid} > /dev/null 2>&1

If the first 'kill' works ${mypid} will no longer be valid and the second (and third) 'kill's will be no-ops. If the 'kill -9' fails to terminate the process then it is waiting on an I/O to complete or in some other kernel state for which it can't be interrupted.

Regards!

...JRF...
Raj D.
Honored Contributor

Re: Killing parent pid

Srinikalyan,

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

Which version of OS you are using.
If you are on 11.31 use , ptree , it is a very nice program. It prints the complete process tree hierarchy.

http://docs.hp.com/en/B2355-60130/ptree.1.html


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "

Re: Killing parent pid

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

Using "--" is overkill and not as flexible as just "-e -u". After -e, you can add more options.

>do NOT grep for pid or ppid!!! This will find critical processes that happen to have matching numbers in the run string.

My grep pattern anchored it to the PPID.