如何在 ruby Kernel.exec 之后运行代码 [英] How to run code after ruby Kernel.exec
问题描述
我有以下 ruby shell.
I have the following ruby shell.
#!/usr/bin/env ruby
$stdin.each_line do |line|
pid = fork{
exec line
puts "after exec -> #{Process.pid}"
}
Process.wait pid
end
exec
之后的 puts
方法永远不会被执行.基于 ri Kernel.exec
,似乎 exec
通过运行给定的外部来替换当前进程.因此,它应该用外部进程替换新的分叉进程.我应该如何在 exec
命令之后运行任何东西?
The puts
method after exec
is never executed. Based on ri Kernel.exec
, it seems that exec
replaces the current process by running the given external. So, it's supposed to replace the new forked processes with the external processes. How am I supposed to run anything after exec
command?
推荐答案
你不能.
根据 内核的文档#exec
, "[it] 通过运行给定的外部命令替换当前进程".这意味着您不再运行您的代码,而是您通过命令指定的代码.
You cannot.
Per the documentation for Kernel#exec
, "[it] replaces the current process by running the given external command". That means that you are no longer running your code but instead the code you specified by the command.
如果你想包装"一个系统调用,那么你应该使用 Kernel#system
(或 反引号运算符) 在子shell中执行命令.
If you want to "wrap" a system call then you should use Kernel#system
(or the backtick operator) to execute the command in a subshell.
这篇关于如何在 ruby Kernel.exec 之后运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!