How to fire and forget a subprocess?

前端 未结 5 1334
深忆病人
深忆病人 2021-01-31 04:28

I have a long running process and I need it to launch another process (that will run for a good while too). I need to only start it, and then completely forget about it.

5条回答
  •  半阙折子戏
    2021-01-31 05:08

    Alnitak is right. Here's a more explicit way to write it, without $$

    pid = Process.fork
    if pid.nil? then
      # In child
      exec "whatever --take-very-long"
    else
      # In parent
      Process.detach(pid)
    end
    

    The purpose of detach is just to say, "I don't care when the child terminates" to avoid zombie processes.

提交回复
热议问题