From: sanjayayogi Date: 2009-04-04T14:12:08+09:00 Subject: Re: Kill a process by name and PID - needs refinement Cleaned up the commenting because of the formatting changes after posting: #!/usr/bin/ruby -w # kill_pid.rb - kill a process by a given name # Written by sanjaya.y...@gmail.com # This code first asks you the name of the process you want to kill - # respond with the name of the process # It will return the pid - if you you only want the pid comment out # the final line: # ie: #exec killall_pid def getBinding(str) return binding end puts " Hello, what PID do you want? " process = gets.chomp a = process # puts a b = "'ps -C " + a + " -o pid='" # puts b D = "IO.popen(" + b + ", 'r+') do |pipe|" str = D + "\n" + "pipe.close_write" + "\n" + "puts pipe.read" + "\n" + "end" #puts str def pid x = D + "\n" + "pipe.close_write" + "\n" + "puts pipe.read" + "\n" + "end" end kill = "kill -9 " eval pid # killall -i makes the code interactive killall_pid = "killall -i " + a puts killall_pid exec killall_pid # kills the process by name - not pid (does not seem to work with all # processes - I think) # it would be better written to kill the process by pid instead but I # do not know how to # take the out put of eval pid and use the kill -9 command with the # pid IDEAS ANYONE?