Azevedo Azevedo - 3 months ago 13
Node.js Question

NodeJS: How to run an standalone process (no-child)

I'm trying to code a function to restart the node server. It basically run the command that kills the current node's PID and run it again with the original command line after 5s.

The problem is that when the node process is killed it also kills the child process that is waiting those 5s before run it again:

const spawn = require('child_process').spawn;
spawn('cmd.exe', ['/C taskkill /F /PID ' + process.pid + ' & ping 127.0.0.1 -n 4 > nul & <command line to launch node again>']);


Is there a way to run a standalone process in nodejs without being a child process?

Answer

I think you can you the detached option:

spawn('cmd.exe', ['/C taskkill /F /PID ' + process.pid + ' & ping 127.0.0.1 -n 4 > nul & <command line to launch node again>'],{'detached':true});

https://nodejs.org/api/child_process.html#child_process_options_detached