Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start an entirely new process in node.js (not child)?

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?

like image 711
Shai UI Avatar asked Mar 11 '14 19:03

Shai UI


People also ask

How do I close a Node.js child process?

kill(-pid) method on main process we can kill all processes that are in the same group of a child process with the same pid group. In my case, I only have one processes in this group. var spawn = require('child_process'). spawn; var child = spawn('my-command', {detached: true}); process.

What is the difference between Node.js child process and clusters?

In a single thread, the individual instance of node. js runs specifically and to take advantage of various ecosystems, a cluster of node. js is launched, to distribute the load. With the help of a cluster module, child processes can be created very easily sharing the server ports.


1 Answers

You 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

like image 134
Jay Avatar answered Oct 04 '22 01:10

Jay