The experiences of an Australian student who uses Linux.
Cover all topics from shell scripts to monopolies and reviews to political comments.
Cover all topics from shell scripts to monopolies and reviews to political comments.
MEGAkill - the process anihilator
Posted 08-28-2010 at 02:52 AM by William (Dthdealer)
Updated 08-28-2010 at 03:05 AM by William (Dthdealer) (Spelling error - captial > capital)
Updated 08-28-2010 at 03:05 AM by William (Dthdealer) (Spelling error - captial > capital)
I'm always fed up when I have to grep through ps -A output or stare at top for ages, trying to find the name or pid of a hung process. Finally today I decided to end my woes with this shell script:
Description
MEGAkill is a shell script that automates the locating of processes by their name before asking the user if they want to kill them.
Usage: megakill keywordofprocess [ signal to send ]
The script greps the output of ps -A and then checks how many processes match. If only one match exists, it is printed to screen the user is asked a Y/N on whether or not to kill it. If multiple matches are found, the user inputs a numeric choice or types anything else to abort.
The script informs the user when no action has been taken if the user aborts or chooses no.
An optional signal argument can also be given. Examples are '9' or 'KILL' to immediately kill a process without letting it close itself down ( very useful for when a process is completely hung). Check the kill manpage ( man kill ) for the whole list of available signals and their meanings.
It does not provide you with any features not normally available, but makes the process nicer and simpler.
Description
MEGAkill is a shell script that automates the locating of processes by their name before asking the user if they want to kill them.
Usage: megakill keywordofprocess [ signal to send ]
The script greps the output of ps -A and then checks how many processes match. If only one match exists, it is printed to screen the user is asked a Y/N on whether or not to kill it. If multiple matches are found, the user inputs a numeric choice or types anything else to abort.
The script informs the user when no action has been taken if the user aborts or chooses no.
An optional signal argument can also be given. Examples are '9' or 'KILL' to immediately kill a process without letting it close itself down ( very useful for when a process is completely hung). Check the kill manpage ( man kill ) for the whole list of available signals and their meanings.
Code:
#!/bin/bash # # MEGAkill - the ultimate lazy man's console process annihilator # # Feel free to redistribute, modify, clone, quote, snip etc this script in any manner as long as: # 1) I, William Hales, am accredited as the original author # 2) No profit can be made from it # 3) Successive authors can change rule (1) so that all successive modifications of their # script ( which is a descendant of mine in the first place ) have rule (1) include not only # my name but their own as well as others if need be. # 4) Any modifications, copies, snippets or future versions fall under these same licence conditions # 5) You are a good person ( your own opinion here is good enough ) # ########*############################################# ########## Argument Syntax Checks \/ \/ \/ ########## if [ -z $1 ] then # User has not passed any arguments echo usage: megakill processname [signal] exit 1 fi ########## Argument Syntax Checks /\ /\ /\ ########## ##################################################### ################################################# ########## Search and Destroy \/ \/ \/ ########## matches=$( ps -A | grep $1 | wc -l ) signal=$2 function killme() { if [ -z $signal ] then # No custom signal specified signaltail="" else signaltail="-s $signal" fi #echo "!!! kill $1 $signaltail" kill $signaltail $1 echo "Process $1 has been annihilated" exit 0 } if [ $matches -eq 0 ] then echo "No grep matches found for '$1'" exit 2 elif [ $matches -eq 1 ] then # Easy job - we only have one match echo -n 'The following process will be killed' if [ -z $2 ]; then echo # Just newline else echo " with signal $2"; fi # Adds the requested signal to question string ps -A | grep $1 # Prints the process and information echo 'Do you wish to continue with capital punishment? [y/N]' read userchoice if [ $userchoice = y -o $userchoice = Y -o $userchoice = yes -o $userchoice = Yes -o $userchoice = YES ] then # User definately wants that process dead tokillid=$(ps -A | grep $1 | cut -f1,2 -d' ') killme $tokillid else # User disagrees echo 'Process not killed' exit 0 fi else # Harder - we have multiple echo "There are $matches processes that match '$1'" ps -A | grep $1 echo -n 'Which do you want to kill [ 1, 2, 3, etc, anything else for NO ]' if [ -z $2 ]; then echo ' ?' # Just newline else echo " with signal $2 ?"; fi # Adds the requested signal to question string read userchoice if [ $userchoice -ge 1 -a $userchoice -le $matches ] then # Find which one in the list is number USERINPUT so we can kill it tokillid=$( ps -A | grep $1 | head -$userchoice | tail -1 ) killme $tokillid else # User wrote something else, so just quit echo 'No actions taken' exit 0 fi fi ########## Search and Destroy /\ /\ /\ ########## #################################################
Total Comments 3
Comments
-
Additional details
Some examples of use:
Quote:whales ~ $ megakill mousepad
The following process will be killed
15714 ? 00:00:00 mousepad
Do you wish to continue with capital punishment? [y/N]
y <ENTER>
Process 15714 has been annihilatedQuote:whales ~ $ megakill bash
There are 4 processes that match 'bash'
1944 tty1 00:00:00 bash
2303 tty2 00:00:00 bash
15709 pts/0 00:00:00 bash
15737 pts/1 00:00:00 bash
Which do you want to kill [ 1, 2, 3, etc, anything else for NO ] ?
4 <ENTER>
Process 15737 has been annihilatedQuote:whales ~ $ megakill bash
There are 4 processes that match 'bash'
1944 tty1 00:00:00 bash
2303 tty2 00:00:00 bash
15709 pts/0 00:00:00 bash
15737 pts/1 00:00:00 bash
Which do you want to kill [ 1, 2, 3, etc, anything else for NO ] ?
n <ENTER>
/home/whales/Lang/bin/megakill: line 89: [: n: integer expression expected
No actions takenPosted 08-28-2010 at 03:03 AM by William (Dthdealer)
Updated 08-28-2010 at 03:05 AM by William (Dthdealer) (Spelling error - captial > capital) -
I can see how it would be a bit different from "pkill bash", for example, but, contrarily to what the name suggests, it wouldn't be that much in the "massacre" aspect, but rather that, instead, it would be easier to kill only the one(s) that "deserve" it or spare some for one reason or another. But without having the "name" and/or more information of the "potential victims"' PIDs, even if it's "grepped" from an input we gave, it does not seem to be much handier than something more manual, as far as I can imagine. We would still be at risk of killing an "innocent" if we don't know the exact PID of the offending process, just like pkill with broader parameters, only slower.
Posted 08-28-2010 at 06:48 PM by the dsc -
The whole aim was to make the world a little easier for the lazy ( such as myself ) .
Quote:We would still be at risk of killing an "innocent" if we don't know the exact PID of the offending process, just like pkill with broader parameters, only slower.Posted 08-29-2010 at 01:16 AM by William (Dthdealer)