killprocess

#!/usr/bin/perl

#######################################################################
# file name: killprocess                                              #
# kill process by pid                                                 #
#######################################################################

LOOP: {
  my $psresult = `/bin/ps`;
  chomp($psresult);
 
  my @lines = split("/n", $psresult);
 
  my $head = $lines[0];
  $head =~ s/^/s+//;
  $head =~ s//s+$//;
 
  my @header = split(//s+/, $head);
 
  # find PID head
  my $columnIndex = 0;
  foreach $e (@header){
    if($e eq "PID"){
      last;
    }
    $columnIndex++;
  }
 
  my $index = 0;
  foreach $line (@lines){
    if($index == 0){
      print "NO $line/n";
    }else{
      print "$index. $line/n";
    }
    $index++;
  }
 
  if($index - 1 > 0){
    print "0. exit/n";
    print "input no:";
    my $input = <STDIN>;
    chomp($input);
   
    if($input =~ m/[0-9]+/ && (($input > 1 && $input <= $index - 1) || $input == 0)){
      if($input != 0){
        my $target = $lines[$input];
        $target =~ s/(^/s+)|(/s+$)//;
       
        my @items = split(//s+/, $target);
       
        my $pid = $items[$columnIndex];
        my $cmd = "/bin/kill -9 $pid";
        print "$cmd/n";
        system("$cmd");
       
      }else{
        last LOOP;
      }
     
    }else{
      last LOOP;
    }
   
  }else{
    print "No process was found!/n";
    last LOOP;
  }
 
  redo LOOP;
}

exit 0;

你可能感兴趣的:(cmd,kill,header,File,System,input)