If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Linux: How to kill all childs running in background?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-04-03, 11:09
jsander jsander is offline
Registered User
 
Join Date: Apr 2003
Posts: 191
Linux: How to kill all childs running in background?

Hi,

in AIX, I believe I can kill off all background processes I started with killall, like:

$ my-db-stress-script &
$ my-db-stress-script &

$ killall

But I have great trouble to come up with a similar solution for Linux, even within pdksh in linux.

Does anybody know if there is a command in Linux and AIX that lets me kill my background processes easily, and if not, how to do this in Linux alone?

Johann
Reply With Quote
  #2 (permalink)  
Old 12-08-03, 17:10
ika ika is offline
Registered User
 
Join Date: Oct 2003
Location: Slovakia
Posts: 482
Re: Linux: How to kill all childs running in background?

Quote:
Originally posted by jsander
Hi,

in AIX, I believe I can kill off all background processes I started with killall, like:

$ my-db-stress-script &
$ my-db-stress-script &

$ killall

But I have great trouble to come up with a similar solution for Linux, even within pdksh in linux.

Does anybody know if there is a command in Linux and AIX that lets me kill my background processes easily, and if not, how to do this in Linux alone?

Johann
1. killall -9 my-db-stress-script
2. ps -aux | grep my-db-stress-script and then kill -9 pid (process id)
Reply With Quote
  #3 (permalink)  
Old 01-06-04, 09:57
astralvoid astralvoid is offline
Registered User
 
Join Date: Feb 2003
Posts: 4
Re: Linux: How to kill all childs running in background?

Quote:
Originally posted by jsander
Hi,

in AIX, I believe I can kill off all background processes I started with killall, like:

$ my-db-stress-script &
$ my-db-stress-script &

$ killall

But I have great trouble to come up with a similar solution for Linux, even within pdksh in linux.

Does anybody know if there is a command in Linux and AIX that lets me kill my background processes easily, and if not, how to do this in Linux alone?

Johann
You should be able to do a simple shell script like:

for i in `ps -ef|grep my-db-stress-script|awk '{print $2}'`
do
kill -9 $i
done

This should work on most variants of UNIX.
Reply With Quote
  #4 (permalink)  
Old 01-06-04, 12:09
jsander jsander is offline
Registered User
 
Join Date: Apr 2003
Posts: 191
Re: Linux: How to kill all childs running in background?

Hi ika and astralvoid,

that's along the lines I settled with, but I would like to do something safer than search the process list.

So I take it there is no system command to say something like

kill that process and all its childs in a single stroke

in linux?

Johann
Reply With Quote
  #5 (permalink)  
Old 01-06-04, 12:53
skrishnaswamy skrishnaswamy is offline
Registered User
 
Join Date: Sep 2003
Posts: 7
If you do an fg you can bring the process to the foreground.
if you do a bg you can put it to the background.

Eg
# somescript &
[1] 17921

# kill %1

will kill that process.

Srikanth
Reply With Quote
  #6 (permalink)  
Old 01-06-04, 17:39
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Quote:
Originally posted by skrishnaswamy
If you do an fg you can bring the process to the foreground.
if you do a bg you can put it to the background.

Eg
# somescript &
[1] 17921

# kill %1

will kill that process.

Srikanth
That's a good way to kill one process, but if there's multiple background jobs, some of which you don't want to kill, it'll not work.

The shell should have a 'jobs' command which lists all the commands running in the background for this instance of the shell, rather than the machine's whole process table. You can munge that output and kill the relevant ones:

for i in `jobs | grep my-db-stress-script | sed -e 's/\].*$//' -e 's/^.*\[//'`; do eval kill %$i; done

For killing a particular process, "kill <pid>" works as in most *nix.

"kill -9" is too aggresive a command to remove a process and all it's children, it kills the process immediately. "man kill" or "man signal" should give you more info on the severity of signals.
Reply With Quote
  #7 (permalink)  
Old 01-06-04, 18:00
skrishnaswamy skrishnaswamy is offline
Registered User
 
Join Date: Sep 2003
Posts: 7
hmmm....on linux you could also use the pstree -p <username> command to find your bg process ,pipe them to a sed and kill em. In that way you wont miss the child pids forked by your background processes and would not reult in Zombie processes.
Reply With Quote
  #8 (permalink)  
Old 01-07-04, 03:47
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
There's a race condition in there, skrishnaswamy. What would happen if, between the output of pstree and the kill commands, one of the processes forks a child? pstree won't have listed it.

It's better to use the kernel to find all of the process' children (its response to the signal) rather than a userspace utility.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On