Process.fork{}
当block为空的时候,fork会返回2次结果,一次是在父进程中,返回子进程的pid,一次是在子进程中,返回nil。
Creates a subprocess. If a block is specified, that block is run in the subprocess, and the subprocess terminates with a status of zero.
Otherwise, the fork call returns twice, once in the parent, returning the process ID of the child, and once in the child, returning nil.
# Process.fork 会运行两次,第一次返回子进程pid,第二次返回nil
pid = Process.fork
puts "current pid :#{pid}"
if pid.nil? then
puts "# In child"
puts exec('ls ~')
else
puts "# In Parent"
puts "Sub Process ID:#{Process.wait(pid)}"
end
Source: http://stackoverflow.com/questions/806267/ruby-how-to-fire-and-forget-a-subprocess