node.jsgruntjschild-processyeti

Detached child process exits on script exit in node.js


I've followed the instructions in the api docs as far as I can tell. I spawn the process, using the options detached:true, stdio:['ignore','ignore','ignore'], I call unref on the ChildProcess. The ChildProcess has a pid, so I think it was successfully started. I'm trying to start a yeti server from within a grunt task. This code is within an async call, so next proceeds and eventually finishes the task. I use which to getcmd, and its the correct path to the yeti script in /usr/local/bin on Mac OSX 10.9. Port is also defined to an empty port. If I run the same command on the command line it works just fine. After the grunt exits I call ps aux | grep node and ps aux and grep for the logged pid and there is nothing running. Here is the code:

yeti = spawn("" + cmd + " --server --port " + port, [], {
  detached: true,
  stdio: ['ignore', 'ignore', 'ignore']
});
yeti.unref();
next("Yeti server is running. pid: " + yeti.pid);

Solution

  • Try this:

    spawn(cmd, ["--server", "--port", port], {
      detached: true,
      stdio: ['ignore', 'ignore', 'ignore']
    });
    

    I've not seen a single example of spawn where everything was passed as the first argument. I've always seen it used with the first argument being only the command name (i.e. the executable name, or path to executable) and the second argument being an array of strings.

    The fact that you were seeing a pid is not indicative of much because on Unix-type systems, the spawn will fork and then exec. The fork can be successful so you see a new pid but the exec fails because the executable's name makes no sense to the OS.