Advertisement
Guest User

GarciaPL

a guest
Aug 10th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.37 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5. use Getopt::Long;
  6. use Data::Dumper;
  7.  
  8. my $help;
  9. my $process_pattern = undef;
  10.  
  11. sub kill_processes {
  12.     if (defined($process_pattern)) {
  13.         my @processeslist = `ps -ef | grep $process_pattern | grep -v grep | awk '{print \$2}'`;
  14.  
  15.         if ((@processeslist) and (scalar(@processeslist) > 1)) {
  16.             print "\nKilling processes with PID like : \n";
  17.             print @processeslist;
  18.             my @killprocess = `kill -9 @processeslist`;
  19.             print Dumper @killprocess;
  20.         } else {
  21.             print "\nCan not find processes contain a pattern '$process_pattern'\n";
  22.         }
  23.     } else {
  24.         print "\nProcess name was not defined\n";
  25.     }
  26. }
  27.  
  28. sub usage {
  29.     print "$0 -p <patternProcess>\n";
  30. }
  31.  
  32. sub help {
  33.    print "\nKill All Processes along with pattern\n";
  34.    usage();
  35.    print <<EOT;
  36. -h --help
  37.    print this help message
  38. -p --pattern
  39.    pattern of process
  40. EOT
  41. }
  42.  
  43. sub check_input {
  44.     Getopt::Long::Configure ("bundling");
  45.     GetOptions(
  46.         'help' => \$help,
  47.         'p=s'  => \$process_pattern,            'pattern=s' => \$process_pattern
  48.     );
  49.  
  50. if ($help) { help(); exit; }
  51. if (!defined($process_pattern))
  52.     { print "Put pattern of process! (-h for help)\n"; usage(); exit;}
  53. }
  54.  
  55. ######### MAIN PROGRAM
  56.  
  57. check_input();
  58.  
  59. ######## KILL PROCESSES ALONG WITH PATTERN
  60. print "\nSearching and ending processes contain a pattern '$process_pattern'";
  61. kill_processes();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement