Results 1 to 9 of 9
I have been trying to figure this out for some time now, and I am just about at my wits end. So I am asking for help..
I have a ...
- 08-23-2007 #1Just Joined!
- Join Date
- Aug 2007
- Posts
- 1
Need help with killing multiple PIDs..
I have been trying to figure this out for some time now, and I am just about at my wits end. So I am asking for help..
I have a cluster, that when a job is finished, is not killing all the left over processes. So I am left with something like this.
6652 pts/2 00:00:00 bash
6674 pts/3 00:00:00 bash
6753 pts/3 00:00:00 magma3234
6754 pts/3 00:00:00 magma23424
6755 pts/3 00:00:00 magma493844
6756 pts/3 00:00:00 magma3954755
6757 pts/3 00:00:00 ps
I need the script to be able to pick up on those random process names and killall -9 them.
Somebody, please help me.
Joshua
- 08-23-2007 #2Linux Newbie
- Join Date
- Dec 2005
- Location
- Toronto
- Posts
- 127
Though this not a solution to your problem, I thought I'd mention something that came up my mind.
Logically thinking, when a process is finished processing it should terminate automatically and cleanly. If you have many processes lurking around even after you think the job is finished you might wanna find out what is going on. You wouldn't want to corrupt data by killing the processes, they might just be waiting for an event to happen or for I/O, not sure !
If you think this is irrelevant, just ignore it !
- 08-23-2007 #3
The real answer is to fix the job so that it's not leaving around a bunch of orphaned processes.
Assuming you're referring to the processes that begin with 'magma', you can kill them with:
# pkill 'magma'
It's possible to fine-tune that command significantly (using regexps) if the above is casting too wide a net.
- 08-24-2007 #4
Funny thing, it had taken me quite some time before I realized ctrl-z doesn't end a process, it just stops it and puts it in the background. Don't know if something like this is the case in your situation though. Just thought I'd mention it.
Anyway. Because of this, I made a really devastating script. You can have it, but use it with care.
This script is old, it's UGLY, it's WAY to complex, and it can mess up your system. Still, it does the job like a laser guided H-bomb.Code:#!/bin/bash for i in `ps aux | grep -i $1 | grep -v grep | awk '{print $2}'`; do kill -9 $i done exit 0
It just kills everything it would find when you'd <ps aux | grep -i FOO>
That means you don't have to write out the entire name, making it quite useful when you're lazy. Some programs do file bug reports when you use this script on them though (sorry people of Firefox and OpenOffice.org, it was me
)
Can't tell an OS by it's GUI
- 08-24-2007 #5Linux Newbie
- Join Date
- Dec 2005
- Location
- Toronto
- Posts
- 127
Don't you think the above script is likely to kill itself. Well, it's a different matter if gets killed just when it's done its work !!#!/bin/bash
for i in `ps aux | grep -i $1 | grep -v grep | awk '{print $2}'`; do
kill -9 $i
done
exit 0
- 08-24-2007 #6That's where the grep -v grep comes in
Originally Posted by kevkim55
Can't tell an OS by it's GUI
- 08-24-2007 #7Linux Newbie
- Join Date
- Dec 2005
- Location
- Toronto
- Posts
- 127
That would filter out the grep but not the comand line itself !That's where the grep -v grep comes in
If we call the script 'killer-script.sh' and execute it like this:
killer-script.sh dummy-prog
'grep -v grep' would filter out the grep - 'grep -i $1' !
Output of 'grep -i $1' would also include the command/process 'killer-script dummy-prog' !
Let me know if I'm wrong !
- 08-24-2007 #8
What kevkim55 is saying is given:
./script_name argument
The process table will contain 'argument', so any grep that seeks to match 'argument' will grab the script's entry/PID (which will be killed -- possibly before it finishes its job).
Just use pkill and make your life easy.
- 08-24-2007 #9Hey! you're right!
Originally Posted by kevkim55
Ain't that something? The first ever script that works while it shouldn't. And I wrote it! Will I be famous? Should I go into development?
===============
To tell you the truth, you where right all along. The script cycles through all the matches and kills them. When it finally finds itself, it's already done. It's always the last in line. No use putting an exit in it then, but for the rest it works. Told ya it was UGLY
Can't tell an OS by it's GUI


Reply With Quote