I would like to share, in my opinion, quite helpful tool dedicated for Linux administrators for ending (or some of you would say "killing") processes which contain particular pattern. I prepared a dedicated script written in Perl which is very easy to help. If you would like to kill all processes contain word "chrome" just run like this one : ./KillProcesses.pl -p chrome. I also wrote a small program with infinity loop which can be used for testing purposes. It can be found using pattern LoopProcess.
KillProcesses.pl
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
my $help;
my $process_pattern = undef;
sub kill_processes {
if (defined($process_pattern)) {
my @processeslist = `ps -ef | grep $process_pattern | grep -v grep | awk '{print \$2}'`;
if ((@processeslist) and (scalar(@processeslist) > 1)) {
print "\nKilling processes with PID like : \n";
print @processeslist;
my @killprocess = `kill -9 @processeslist`;
print Dumper @killprocess;
} else {
print "\nCan not find processes contain a pattern '$process_pattern'\n";
}
} else {
print "\nProcess name was not defined\n";
}
}
sub usage {
print "$0 -p \n";
}
sub help {
print "\nKill All Processes along with pattern\n";
usage();
print < \$help,
'p=s' => \$process_pattern, 'pattern=s' => \$process_pattern
);
if ($help) { help(); exit; }
if (!defined($process_pattern))
{ print "Put pattern of process! (-h for help)\n"; usage(); exit;}
}
######### MAIN PROGRAM
check_input();
######## KILL PROCESSES ALONG WITH PATTERN
print "\nSearching and ending processes contain a pattern '$process_pattern'";
kill_processes();
LoopProcess.pl
#!/usr/bin/perl -w
use strict;
use warnings;
while (1) {
}
Reference : [1] GarciaPL Pastebin KillProcesses [2] GarciaPL Pastebin LoopProcess
Killing processes contain pattern
2013-08-10T11:27:00+01:00
GarciaPL
Perl|Ubuntu|
