How do you kill PTY.spawn call in Ruby?

If I run a command like this using the ruby ​​pty class, how do I kill it if I find a specific input line?

cmd = "appcfg.py update cdn"
PTY.spawn("#{cmd} 2>&1") do | input, output, pid |
  begin
    input.expect("Email:") do
      output.write("#{credentials[:username]}\n")
    end
    input.expect("Password:") do
      output.write("#{credentials[:password]}\n")
    end
    if input.gets == "SOMETHING"
      EXIT!
    end
  rescue Exception => e
    puts "GAE Error..."
  end
end

      

What is the correct way to do this?

+2


source to share


1 answer


How about this?

processes =  %x[ps -A].split("\n")
processes.each do |p|
  if p.include?('ruby1.9')
    pid = p.split(' ')
    %x[kill #{pid[0].to_i}]
  end
end

      



This is of course if you run ruby1.9 (and it will kill ALL ruby1.9 processes!), So if you are using some other ruby ​​prog on your server or something, you will need to do some kind of if statement to check which process it is.

+1


source







All Articles