Forum: Ruby Kill a process by name and PID - needs refinement

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6627c5bbcca7444b28002d1bd3c574ac?d=identicon&s=25 sanjayayogi (Guest)
on 2009-04-04 07:04
(Received via mailing list)
#!/usr/bin/ruby -w
# kill_pid.rb - kill a process by a given name
# Written by sanjaya.yogi@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"
#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
6627c5bbcca7444b28002d1bd3c574ac?d=identicon&s=25 sanjayayogi (Guest)
on 2009-04-04 07:13
(Received via mailing list)
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?
6627c5bbcca7444b28002d1bd3c574ac?d=identicon&s=25 sanjayayogi (Guest)
on 2009-04-04 11:50
(Received via mailing list)
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 sanjaya.y...@gmail.com 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
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2009-04-04 12:07
(Received via mailing list)
On Sat, Apr 4, 2009 at 3:19 PM, sanjayayogi <sanjaya.yogi@gmail.com>
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
This topic is locked and can not be replied to.