#!/usr/bin/perl
use strict;
#
# yesno returns 1, if the user entered 'y' or 'Y'
#
sub yesno
{
print "$_[0] y/[n]: ";
return (<STDIN> =~ /^(y|Y)$/);
}
#
# main
#
my $line;
if( @ARGV != 1)
{
print <<EOF;
Usage:
kill_proc search_string
EOF
;
exit 1;
}
for $line (`ps ax`)
{
my ($pid, $tty, $stat, $time, @rest) = split ' ', $line;
my $command = join ' ', @rest;
#
# test whether the search string is part of the command field
# exclude the current process from being killed
#
if( $command =~ /.*$ARGV[0].*/ &&
!( $command =~ /.*kill_proc.*/))
{
if( yesno( "Kill '$command'"))
{
kill 9, $pid;
}
}
}
exit 0;