如何获取PTY.spawn子退出代码?

dev*_*arn 5 ruby linux io ssh

我正在尝试通过PTY模块管理到网络设备的SSH连接,代码类似于:

cmd_line = "ssh coltrane@love.supreme.com"
begin
  PTY.spawn(cmd_line) do |r_f,w_f,pid|
  ...
rescue PTY::ChildExited => cended
  ...
end
Run Code Online (Sandbox Code Playgroud)

整个I/O工作得很好,但是我不知道如何获得子进程的退出状态.

例如,如果连接断开或只是超时,则生成的进程将以错误代码终止,但此代码似乎不会在$?特殊变量中返回.

Sim*_*ang 15

TLDR

使用1.9.2并等待PTY进程正确设置$?

PTY.spawn(command) do |r,w,pid|
  # ...
  Process.wait(pid)
end
Run Code Online (Sandbox Code Playgroud)

全文

在1.9.2上,您可以通过在PTY pid上调用wait来捕获PTY的退出状态.几乎所有时间(AFAIK)都有效.我所知道的唯一例外是边缘情况,例如立即退出或为命令发出空字符串(请参阅http://redmine.ruby-lang.org/issues/5253).

例如:

require 'pty'
require 'test/unit'

class PTYTest < Test::Unit::TestCase
  def setup
    system "true"
    assert_equal 0, $?.exitstatus
  end

  def pty(cmd, &block)
    PTY.spawn(cmd, &block)
    $?.exitstatus
  end

  def test_pty_with_wait_correctly_sets_exit_status_for_master_slave_io
    status = pty("printf 'abc'; exit 8") do |r,w,pid|
      while !r.eof?
        r.getc
      end
      Process.wait(pid)
    end
    assert_equal 8, status
  end

  def test_pty_with_wait_correctly_sets_exit_status_for_basic_commands
    status = pty("true") do |r,w,pid|
      Process.wait(pid)
    end
    assert_equal 0, status

    status = pty("false") do |r,w,pid|
      Process.wait(pid)
    end
    assert_equal 1, status
  end

  def test_pty_with_wait_sets_exit_status_1_for_immediate_exit
    status = pty("exit 8") do |r,w,pid|
      Process.wait(pid)
    end
    assert_equal 1, status
  end

  def test_pty_with_kill
    status = pty("sleep 10") do |r,w,pid|
      Process.kill(9, pid)
      Process.wait(pid)
    end

    assert_equal nil, status
  end
end
Run Code Online (Sandbox Code Playgroud)

现在运行测试:

$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]
$ ruby example.rb
Loaded suite example
Started
....
Finished in 1.093349 seconds.

4 tests, 9 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 31924
Run Code Online (Sandbox Code Playgroud)

在1.8.7你需要做更多.在较旧的rubies中,即使您等待PTY进程完成,PTY也会经常以PTY :: ChildExited错误退出.因此,如果您按照编写的方式运行测试,则得到以下结果:

$ ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]
$ ruby example.rb
Loaded suite example
Started
EE.E
Finished in 1.170357 seconds.

  1) Error:
test_pty_with_kill(PTYTest):
PTY::ChildExited: pty - exited: 35196
    example.rb:11:in `test_pty_with_kill'
    example.rb:11:in `spawn'
    example.rb:11:in `pty'
    example.rb:45:in `test_pty_with_kill'

  2) Error:
test_pty_with_wait_correctly_sets_exit_status_for_basic_commands(PTYTest):
PTY::ChildExited: pty - exited: 35198
    example.rb:11:in `test_pty_with_wait_correctly_sets_exit_status_for_basic_commands'
    example.rb:11:in `spawn'
    example.rb:11:in `pty'
    example.rb:26:in `test_pty_with_wait_correctly_sets_exit_status_for_basic_commands'

  3) Error:
test_pty_with_wait_sets_exit_status_1_for_immediate_exit(PTYTest):
PTY::ChildExited: pty - exited: 35202
    example.rb:11:in `test_pty_with_wait_sets_exit_status_1_for_immediate_exit'
    example.rb:11:in `spawn'
    example.rb:11:in `pty'
    example.rb:38:in `test_pty_with_wait_sets_exit_status_1_for_immediate_exit'

4 tests, 5 assertions, 0 failures, 3 errors
Run Code Online (Sandbox Code Playgroud)

注意几乎所有的测试都有一个ChildExited错误炸弹,但是一个(顺便说一下,代表最真实的PTY使用的那个)成功按预期成功.当然,这种不稳定的行为是一个错误,正如已经显示的那样,它已在1.9.2中得到修复.

但是,有部分解决方法.您可以使用以下内容专门处理ChildExited错误:

def pty(cmd, &block)
  begin
    PTY.spawn(cmd, &block)
    $?.exitstatus
  rescue PTY::ChildExited
    $!.status.exitstatus
  end
end
Run Code Online (Sandbox Code Playgroud)

插入它,再次运行测试,你得到的结果与1.9.2一致,BIG警告说$?将无法正确设置(与1.9.2不同).特别是如果您要添加此测试:

def test_setting_of_process_status
  system "true"
  assert_equal 0, $?.exitstatus

  begin
    PTY.spawn("false") do |r,w,pid|
      Process.wait(pid)
    end
  rescue PTY::ChildExited
  end
  assert_equal 1, $?.exitstatus
end
Run Code Online (Sandbox Code Playgroud)

你在1.9.2上取得了成功,你在1.8.7上失败了.在1.8.7的情况下,PTY通过ChildExited错误完成 - Process.wait永远不会被调用,因此永远不会设置$?.相反,$?从'system"true"'继续,你得到0而不是1作为退出状态.

$的行为?很难遵循,并有更多的警告,我不会进入(即有时PTY 通过Process.wait完成).


Abo*_*our -1

摘要:在 Linux 中,父进程应该 wait() 来让子进程知道其子进程的退出状态。
C代码:

int 状态;
wait(&status) // 在父级代码部分
WEXITSTATUS(status) //返回子级退出代码的宏

抱歉,我没有使用 ruby​​ 的经验,无法为您提供一些代码。最好的祝愿 :)