Operating System - HP-UX
1819932 Members
3253 Online
109607 Solutions
New Discussion юеВ

kill parent and its child process

 
prakasse
Advisor

kill parent and its child process

I am working on HP-UX 11.23 Integrity server. I would like to know the command which will help to kill the parent process as well as the child process of the parent. Kill -9 kills only the parent process and not its child process.

Thanks in advance,
Senthil.
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: kill parent and its child process

It appears if you aren't in the same process group, you must specify each PID.

Also, you should be very leery of using kill -9.

One way to get the PIDs of a tree is to use:
$ UNIX95= ps -Hfu user

Then you can cut & paste a PID and PPID in one step. Move up 2 lines and cut 2 more.
Ralph Grothe
Honored Contributor

Re: kill parent and its child process

It isn't even a good idea to reap the parent that way because you make the child an orphan.
You will either have to parse all the pids that have the same ppid (i.e. are children of the same parent).
e.g.
$ UNIX95= ps -C sshd -o pid= -o ppid=
990 1
13403 990
13405 13403
2671 2669
2669 990

Here 2669 and 13403 are both children of sshd pid 990.

An alternative, kill several birds with one stone, would be if they all belong to the same process group.
Then it is sufficient to only signal the pgid with a prepended minus sign.

e.g.

$ UNIX95= ps -g 2241 -o pid,ppid,pgid,comm
PID PPID PGID COMMAND
2244 2242 2241 oninit
2241 1 2241 oninit
2245 2242 2241 oninit
2242 2241 2241 oninit
2243 2242 2241 oninit
2246 2242 2241 oninit
2247 2242 2241 oninit
2248 2242 2241 oninit
2249 2242 2241 oninit
2250 2242 2241 oninit

Here we have several Informix processes that all belong to the same pgid 2241.
Then you could kill -s TERM -2241
(note you must specify the signal explicitly in that case or prepend -- to indicate that the following minus sign does not indicate an option)
However, one should never finish the database that way.
Madness, thy name is system administration
Steve Steel
Honored Contributor

Re: kill parent and its child process

Hi

Process=$1
plist=$Process
ps -ef|grep $Process|grep -v grep
ps -ef|while read a b c rest
do
if [ "$c" = "$Process" ]
then
plist=$plist" "$b
fi
done
echo kill -9 $plist

If you like the output . Just use the command printed.

This is all good input but as indicated dangerous so dry run a few tests before making a command which just kills


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Arturo Galbiati
Esteemed Contributor

Re: kill parent and its child process

Hi,
I use the attached scripts killkids to kill all the childs of a process including a process itself (-s).
It run on hpux11i from years.

HTH,
Art
Arturo Galbiati
Esteemed Contributor

Re: kill parent and its child process

Hi,
this is the script (pidtree) used by the previous script.
Surely both can be improved. Please, in case someone improve them sent me a copy.

HTH,
Art
prakasse
Advisor

Re: kill parent and its child process

I have found a solution to this question as seen in the comments below