Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 <patternProcess>\n";
- }
- sub help {
- print "\nKill All Processes along with pattern\n";
- usage();
- print <<EOT;
- -h --help
- print this help message
- -p --pattern
- pattern of process
- EOT
- }
- sub check_input {
- Getopt::Long::Configure ("bundling");
- GetOptions(
- 'help' => \$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();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement