简体   繁体   中英

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?

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


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM