Вопрос

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);
Это было полезно?

Решение

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.

Другие советы

If you found this while investigating why your process exits when you kill the parent, though it's detached, and you're using PM2 : https://github.com/Unitech/pm2/issues/1036

pm2 uses kill process tree. This kills the entire process tree. Running cli commands with --no-treekill will solve this.

related also: https://github.com/Unitech/PM2/issues/1564

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top