开发者

How do you kill a PTY.spawn call in Ruby?

开发者 https://www.devze.com 2022-12-26 18:25 出处:网络
If I run a command like this, using ruby\'s pty class, how do I kill it if I find a certain input string?

If I run a command like this, using ruby's pty class, how do I kill it if I find a certain input string?

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 right way to do that?


What about something like 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 ofcourse if you run ruby1.9 (and it will kill ALL ruby1.9 processes! (so if you are running some other ruby prog on your server or something you will have to do some if statement to check which process it is.

0

精彩评论

暂无评论...
验证码 换一张
取 消