LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2009, 12:35 AM   #1
bpteoh
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
Smile How to trace the process id and killl it together with its child processes.


I got one prewriten C application, which can run multiple instance in linux, each will occupied a port for data transfering.
In some case when certain port is not working, i need to kill the process, including all its child processes and restart it.

How should i do it? i was thinking to write a script to trace all the process id with its child process , write in a file and kill it when needed. but i don't know how i suppose to write.
 
Old 01-13-2009, 12:44 AM   #2
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by bpteoh View Post
How should i do it? i was thinking to write a script to trace all the process id with its child process , write in a file and kill it when needed.
check pstree command. may be it is helpful to you.....
 
Old 01-13-2009, 04:40 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi, bpteoh.

Try the following bash script:
Code:
#!/bin/bash
# file: listchild.sh
# Usage: listchild.sh PID

#set -x

: ${start:=$1}
while [ -n "$start" ];
do
	echo $start; 
	# replace spaces/newlines by commas
	start=`echo "$start"| tr '\n' ' ' |sed 's/ \+/,/g'`;
	# eat up leading comma
	start=${start:0:((${#start}-1))};
	start=`pgrep -P$start`;
done
Uncomment 'set -x' line to see how the script works. Command `pgrep -P<comma-separated-list-of-pids>' will print newline-separated list of pids, whose parents were listed. On Debian system pgrep can be found in the procps package.

Command `ps -Af --forest` may be useful for checking the result.

To kill all child processes of given one, type:
Code:
kill `./listchild.sh PID`

Last edited by firstfire; 01-13-2009 at 04:51 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
About the signal after trace the child process youngtrips Programming 2 05-29-2008 09:44 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration