#!/usr/bin/perl
#$command is the command to grep from the ps list
#change $username and $command corresponding to the user running jboss
#$command = "run.sh";
$command = "java";
$username = "ykc";

$counter = @ARGV;

if($counter != 0 ){
   print "./kill_server";
   print "\n";
   exit;
}


@pid_lines = `ps -u $username| grep $command `;


$counter = @pid_lines;
@pid;
$i = 0;
@ps_tokens;

   
for($i=0; $i<$counter; $i++){
   chomp($pid_lines[$i]);
   $pid_lines[$i] = ltrim($pid_lines[$i]);
   @ps_tokens=split(/\s+/,$pid_lines[$i]);
   
   $pid[$i] = $ps_tokens[0];
  
   }


kill_pid(@pid);
   
   sub ltrim($)
   {
      my $string = shift;
      $string =~ s/^\s+//;
      return $string;
   }

   # kill -9 the first process from the ps list
   sub kill_pid(@)
   {
     for($i=0; $i<$counter; $i++){
      print "pid to kill = $pid[$i]";    
      system("kill -9 $pid[$i]");
      } 
   }
