Handy little util that searches currently running processes and kills those matching a string or regexp.
It was created as a result of me getting annoyed with continually having to use:
for PROCESS in `/bin/ps aux | grep [n]etscape | sed -e 's/[[:blank:]]\
+{2,\}/ /g' | cut -f2 -d' '`; do kill $PROCESS; done;
to achieve the same effect.
#!/usr/bin/perl -w
require 5;
use strict;
use Getopt::Std;
use vars qw(%opts $term @processes $user $signal);
getopts('ht:s:u:mn', \%opts);
sub usage(){
print <<EOF;
Usage: $0 [options] [search term]
Example: $0 -m -s STOP -t netscape
-h : displays this help screen
-t TERM : search for TERM
-n : do not kill anything, merely list process ids
-s SIGNAL : send processes SIGNAL (default is TERM)
-m : only kill processes owned by you
-u USER : only kill processes belonging to USER
If no options are given, option t is assumed if a search term is prese
+nt, option h otherwise.
EOF
exit;
}
if ($opts{'h'}){
usage();
}
$user = $opts{'u'} || ($opts{'m'} && getpwuid($<));
if ($term = ($opts{'t'} || $ARGV[0])) {
print "Processes matching \'$term\'", defined($user) ? " and owned
+ by \'$user\'": "" ,": ";
@processes = map { m@^(\w+)\s+(\d+)\s+[\d\.]+\s+[\d\.]+\s+.+$term.
+*$@ && ($2 != $$) && (!(defined ($user)) || ($1 eq $user))? $2 : () }
+ `ps aux`;
if (@processes){
print "@processes\n";
} else {
print "none\n";
exit;
}
if (!$opts{'n'}){
$signal = defined($opts{'s'}) ? $opts{'s'} : 15;
kill $signal, @processes;
print "Done killing processes\n"
}
} else {
usage();
}
In reply to killgrep
by kilinrax
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.