3

rubyのptyクラスを使用してこのようなコマンドを実行した場合、特定の入力文字列が見つかった場合、どうすればそれを強制終了できますか?

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

それを行う正しい方法は何ですか?

4

1 に答える 1

1

このようなものはどうですか?

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

もちろん、これはruby1.9を実行する場合です(そして、すべてのruby1.9プロセスを強制終了します!(したがって、サーバーまたは何かで他のrubyプログラムを実行している場合は、ifステートメントを実行してどのプロセスであるかを確認する必要があります。

于 2010-04-12T12:54:41.703 に答える