如何确定 PTY.spawn 在 ruby 脚本中完成的时间,以便启动新进程
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了帮助您调试,我稍微减少了代码,并生成了一个更良性的命令:
此输出:
这对我来说意味着问题出在 apt-get 命令中。
To help you debug, I reduced the code a bit, and spawned a more benign command:
This outputs:
This implies to me that the problem lies in the
apt-get
commands.事实上我找到了一个解决方案,它很简单但有点令人惊讶。第二个 apt-get 真正要问的是,
我认为发生的情况是期望与该字符串的第一部分匹配,并在字符串完全输出之前发送“Y”,并且 apt-get 尚未准备好接收“Y”。一旦我将期望匹配更改为上面的字符串(如下面的代码所示),那么一切都正常。
Actually I found a solution and it was very simple but a little surprising. What the second apt-get really asks is
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.