Kill a process by name and PID - needs refinement


#1

#!/usr/bin/ruby -w

kill_pid.rb - kill a process by a given name

Written by removed_email_address@domain.invalid

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”
#put 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

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?
exec killall_pid


#2

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 removed_email_address@domain.invalid

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?


#3

This works pretty well but does not ask you for confirmation before
you clobber a process
Any other improvements welcome. IDEAS? It uses kill -9, probably not
the best idea…

#!/usr/bin/ruby -w

kill_pid.rb - kill a process by a given name

Written by removed_email_address@domain.invalid Sat Apr 4 06:43:02 BRT 2009

This code first asks you the name of the process you want to kill -

respond with the name of the process

It kills the process after you hit return without asking for

confirmation
puts "What is the name of the process you want to kill? "
process = gets.chomp
a = process
#output = “”
#IO.popen(“ps -aef | grep ‘PROCESS_NAME’ | grep -v grep | awk ‘{print
$2}’”) do |readme|

readme.each do |line|

output << line

end

#end
o = “ps -aef | grep " + a + " | grep -v grep | awk ‘{print $2}’”
#puts o
output = “”
D = ‘IO.popen( "’ + o + ‘" ) do |readme|’ + “\n” + “readme.each do |
line|” + “\n” + “output << line” + “\n” + “end” + “\n” + “end” +
“\n”
eval D
kill = "kill -9 "
kill_output = kill + output
#puts kill_output
exec kill_output


#4

On Sat, Apr 4, 2009 at 3:19 PM, sanjayayogi removed_email_address@domain.invalid
wrote:

This works pretty well but does not ask you for confirmation before
you clobber a process
Any other improvements welcome. IDEAS? It uses kill -9, probably not
the best idea…

As a matter of style, you should separate out the part of your program
that does the actual work from the part that does the input/output.
Structure it more like

def get_pid_from_name(process_name)
#…
end

def kill_process(pid, signal)
#…
end

main program starts here

print “enter process name”
pname = gets.chomp()
print “enter signal”
signal = gets.chomp()

pid = get_pid_from_name(pname)
kill_process(pid, signal)

martin