如何确定 PTY.spawn 在 ruby​​ 脚本中完成的时间,以便启动新进程

发布于 2024-12-19 15:08:22 字数 941 浏览 0 评论 0原文

我有一个 Ruby 脚本,它生成一个进程,并使用 Expect 等待输入请求,提供该输入,然后在进程完成时生成一个新进程。

最初的问题是脚本没有等待第一个进程完成,而是执行了脚本中的下一行。

因此,我将 Process.wait(pid) 添加到脚本中,但现在脚本挂起,等待 Process.wait(pid) 处的第二个 PTY.spawn 上的输入。

我正在使用 Ruby 1.9.2 运行脚本,据我所知,有 Ruby 1.8.7 中 PTY.spawn 的一些问题。

该脚本如下所示:

#!/usr/bin/env ruby
require 'pty'
require 'expect'


  PTY.spawn(" apt-get install policycoreutils ") do |reader, writer, pid|
    puts reader.expect(/Do you want to continue/)
    writer.printf("Y\n")
    Process.wait(pid)
  end

  PTY.spawn(" apt-get install libmagick9-dev ") do |reader, writer, pid|
    puts reader.expect(/Do you want to continue/)
    writer.printf("Y\n")
    Process.wait(pid)
  end

有谁知道为什么脚本在第二个 PTY.spawn 处挂在 Process.wait(pid) 处?

I have a Ruby script that spawns a process and uses expect to wait for a request for input, provide that input and then when the process is finished, to spawn a new process.

Initially the problem was that the script didn't wait for the first process to finish, and executed the next line in the script.

So I added Process.wait(pid) to the script, but now the script hangs waiting for input on the second PTY.spawn at Process.wait(pid).

I'm running the script with Ruby 1.9.2, as I know there were some problems with PTY.spawn in Ruby 1.8.7.

The script looks like:

#!/usr/bin/env ruby
require 'pty'
require 'expect'


  PTY.spawn(" apt-get install policycoreutils ") do |reader, writer, pid|
    puts reader.expect(/Do you want to continue/)
    writer.printf("Y\n")
    Process.wait(pid)
  end

  PTY.spawn(" apt-get install libmagick9-dev ") do |reader, writer, pid|
    puts reader.expect(/Do you want to continue/)
    writer.printf("Y\n")
    Process.wait(pid)
  end

Does anyone know why the script hangs at Process.wait(pid) for the second PTY.spawn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-12-26 15:08:22

为了帮助您调试,我稍微减少了代码,并生成了一个更良性的命令:

require 'pty'
require 'expect'

PTY.spawn("sleep 1;echo 1") do |reader, writer, pid|
  puts reader.expect(/1/)
  Process.wait(pid)
end

PTY.spawn("sleep 1;echo 2") do |reader, writer, pid|
  puts reader.expect(/2/)
  Process.wait(pid)
end

此输出:

ruby test.rb 
1
2

这对我来说意味着问题出在 apt-get 命令中。

To help you debug, I reduced the code a bit, and spawned a more benign command:

require 'pty'
require 'expect'

PTY.spawn("sleep 1;echo 1") do |reader, writer, pid|
  puts reader.expect(/1/)
  Process.wait(pid)
end

PTY.spawn("sleep 1;echo 2") do |reader, writer, pid|
  puts reader.expect(/2/)
  Process.wait(pid)
end

This outputs:

ruby test.rb 
1
2

This implies to me that the problem lies in the apt-get commands.

兮颜 2024-12-26 15:08:22

事实上我找到了一个解决方案,它很简单但有点令人惊讶。第二个 apt-get 真正要问的是,

 "Do you want to continue [Y/n]? "

我认为发生的情况是期望与该字符串的第一部分匹配,并在字符串完全输出之前发送“Y”,并且 apt-get 尚未准备好接收“Y”。一旦我将期望匹配更改为上面的字符串(如下面的代码所示),那么一切都正常。

PTY.spawn(" apt-get install libmagick9-dev ") do |reader, writer, pid|
   puts reader.expect(/Do you want to continue \[Y\/n\]\? /)
   writer.printf("Y\n")
   Process.wait(pid)
 end

Actually I found a solution and it was very simple but a little surprising. What the second apt-get really asks is

 "Do you want to continue [Y/n]? "

I think what was happening was that the expect was matching on the first part of this string and sending the "Y" before the string had finished being fully output and the apt-get wasn't ready to receive the "Y". Once I changed the expect match to the string above, as in the code below, then it all worked fine.

PTY.spawn(" apt-get install libmagick9-dev ") do |reader, writer, pid|
   puts reader.expect(/Do you want to continue \[Y\/n\]\? /)
   writer.printf("Y\n")
   Process.wait(pid)
 end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文