开发者

How to detect a process ending with Ruby open3

If I run open with:

input, output,error = Open3.popen3 "nikto -host someip -port 80 -output xml"

How can I detect if nikto is done? It takes a while for the scan to complete.

If something goes wrong, I suppose I have to periodically check error to see if anything that been written?

Is there any decent docs th开发者_如何学Goat exist for open3? No, the ruby-docs are nowhere near decent.


input, output, error = Open3.popen3 "nikto -host someip -port 80 -output xml"

if select([output], nil, nil, 0.1) and output.eof?
  # process exited and doesn't have output queued.
else
  # still running or has output not read yet.
end

Also, here is some good documentation I found by googling: http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/open3/rdoc/index.html


If you're using any *nix OS, your process will receive SIGCHLD when a subprocess exits. Depending on whether or not you have more than one subprocess at a time, this can be used to detect when it ends.

Also, the IO channels to the subprocess are implemented under the hood with pipes, so you will definitely get an EOF at the end of the output and possibly SIGPIPE when it shuts too.

In Ruby, installing a signal handler is just:

Signal.trap("CHLD") do
  Process.wait
  $child_died = true
end


You may be able to get the PID from $?

 Process.wait $?.pid

Turns out that was wrong. See some options here:

http://en.wikibooks.org/wiki/Ruby_Programming/Running_Multiple_Processes


Output is seen after the execution of command finishes

Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
  puts "stdout is:" + stdout.read
  puts "stderr is:" + stderr.read
end

Output is seen incrementally

Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
  while line = stderr.gets
    puts line
  end
end

Check this more options http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜