6

TL;DR : How does one fork a process that is located outside of the current running process?

I'm trying to use child_process of Nodejs in order to start another nodejs process on the parent's process exit.

I successfully executed the process with the exec but I need the child process be independent of the parent, so the parent can exit without waiting for the child, hence I tried using spawn with the detached: true, stdio: 'ignore' option and unref()ing the process:

setting options.detached to true makes it possible for the child process to continue running after the parent exits.

spawn('node MY_PATH', [], {detached: true, stdio: 'ignore'}).unref();

This yields the :

node MY_PATH ENOENT error. which unfortunately I've failed resolve.

After having troubles achieving this with spawn and reading the documentationagain i figured i should actually use fork:

The child_process.fork() method is a special case of child_process.spawn() used specifically to spawn new Node.js processes.

fork() doesnt take a command as its' first argument, but a modulePath which i can't seem to fit since the script I'm trying to run as a child process isnt in the directory of the current running process, but in a dependency of his.

Back to the starting TL;DR - how does one fork a process that is located outside of the current running process?

Any help would be much appreciated!

EDIT:

Providing a solution to the spawn ENOENT error could be very helpfull too!

Kesem David
  • 2,135
  • 3
  • 27
  • 46
  • http://stackoverflow.com/questions/19902828/why-does-enoent-mean-no-such-file-or-directory –  Dec 18 '16 at 15:37
  • Is `MY_PATH` a variable? if it is, you should try: `'node ' + MY_PATH` (or use template string) – GilZ Dec 18 '16 at 15:39
  • @GilZ no, actually its a relative string to `../../node_modules/bla/bla/bla.js` – Kesem David Dec 18 '16 at 15:42
  • Try appending the value of [`__dirname`](http://stackoverflow.com/questions/8131344/what-is-the-difference-between-dirname-and-in-node-js) before your relative path – GilZ Dec 18 '16 at 15:44
  • @GilZ Hey Gil, doing as you suggested provides the same error, it says : `cannot find module CURRENT_PROCESS_DIRECTORY/node ../../node_modules/bla/bla/bla.js` the thing is the `fork` doesnt work like `spawn` / `exec` it recieves a modulePath and not the command, how can i deal with it? – Kesem David Dec 18 '16 at 15:57
  • What I meant was something like: `'node ' + __dirname + '/../../node_modules/bla/bla/bla.js'`. This will anchor your relative path. – GilZ Dec 18 '16 at 16:05
  • @GilZ Are you suggesting this as a solution for the spawn or fork issue ? Cause I can't seem to understand how it will change the fact the fork just seems to append the provided modulePath argument to the CURRENT_PROCESS_DIRECTORY – Kesem David Dec 18 '16 at 16:56
  • @GilZ Hey again, this produces the same error. I would like to clarify again that the child process I would like to fork IS NOT located within the same directory of `__dirname`, it is one directory above it, so appending ../ after `__dirname` obviously wont work – Kesem David Dec 19 '16 at 08:18

1 Answers1

1

Following code should allow you to do what you need.

var Path = require('path');
var Spawn = require('child_process').spawn;

var relative_filename = '../../node_modules/bla/bla/bla.js';

Spawn('node', [Path.resolve(__dirname, relative_filename)], {detached: true, stdio: 'ignore'}).unref();

process.exit(0);
Molda
  • 5,619
  • 2
  • 23
  • 39
  • Hey Molda, using exec I have already had success but I need the prices to continue on running after the parent dies, to have an independent child I figured I need spawn or fork – Kesem David Dec 19 '16 at 21:44
  • I have updated the code with use of Spawn which works OK. – Molda Dec 19 '16 at 22:05
  • Molda thank you, that actually worked. I cant seem to understand why I had to place the path in the arguments array and why won't it work within the command. Moreover, How could it work with `fork`, from the documentation it seems like `fork` is intended specificaly for node child processes (they called it a 'special case of `spawn`'), additional explanation would be much appreciated! – Kesem David Dec 20 '16 at 13:38
  • I don't know the difference in implementation if exec not spawn. In documentation it states that for *exec* you can put arguments in the first string but for *spawn* they need to go into second argument which is array. I do not know much more about it, I just follow documentation. – Molda Dec 20 '16 at 13:53