0

我有以下红宝石外壳。

#!/usr/bin/env ruby

$stdin.each_line do |line|
  pid = fork{
    exec line
    puts "after exec -> #{Process.pid}"
  }
  Process.wait pid
end

putsafter的方法exec永远不会执行。基于ri Kernel.exec,似乎exec通过运行给定的外部来替换当前进程。因此,它应该用外部进程替换新的分叉进程。我应该如何在exec命令之后运行任何东西?

4

1 回答 1

3

你不能。

根据文档Kernel#exec,“[it] 通过运行给定的外部命令替换当前进程”。这意味着您不再运行代码,而是运行命令指定的代码。

如果要“包装”系统调用,则应使用Kernel#system(或反引号运算符)在subshel ​​l中执行命令。

于 2013-09-25T18:40:58.747 回答