The only answers I've seen for starting a process is using something called child_process. But I want to spawn an entirely new process completely independent from my current running node process, is this possible?
The only answers I've seen for starting a process is using something called child_process. But I want to spawn an entirely new process completely independent from my current running node process, is this possible?
Share Improve this question asked Mar 11, 2014 at 19:55 Shai UIShai UI 51.9k77 gold badges217 silver badges316 bronze badges 1- 1 @AfshinMoazami, each process gets a copy of the environment variable at the startup. So the process will never refresh its environment variable? – Tarun Lalwani Commented Aug 24, 2019 at 11:03
3 Answers
Reset to default 16You can spawn a child process in a detached state, ignore the outputs, and remove the child from the parents event loop with child.unref()
.
This code will start someScript.sh
, and exit while keeping someScript.sh
running.
var spawn = require('child_process').spawn;
var child = spawn(__dirname + '/someScript.sh', [], {
detached: true ,
stdio: [ 'ignore', 'ignore', 'ignore' ]
});
child.unref();
For more detailed information and alternatives (such as logging output / etc), take a look at the documentation for spawn
. There are other examples there as well:
http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
Haven't got too much information but it seems like either spawn, exec or fork options for child process. The other option would be a shared port, both options are clarified further here: http://craigbrookes.com/2012/03/17/multiple-processes-in-nodejs/
You can use the natively provided child process facility: http://nodejs.org/api/child_process.html
And use the unix "nohup" command to keep the spawned process alive even if the parent process died.