Start new process on nodejs


I'm working with node.js and I want to when there's a request to a url like ./calculate start a new process to make these complex calculations, and I want that process to continue even if the script which called it has finished. Is it possible?

Thank you.

Problem courtesy of: Mario


You can use the natively provided child process facility:

And use the unix "nohup" command to keep the spawned process alive even if the parent process died.

Solution courtesy of: Roman


There are different approach to this.

You could use

Or much better

Those solutions are if you want to do a subprocess in Node, you could also simply spawn a new Node process and wait for the output ( but node-webworker is a wrapper around that solution and is a much cleaner.

Discussion courtesy of: 3on

So after your last comment I know understand better what you want to do.

You want to spawn process not child process. The best and cleaner way to do this is to use some kind of demonizier to run them.

Have a look at forever:

This could be the perfect tool for you. I've never used it programatically, just cli. But this is what I'd look into.

Discussion courtesy of: 3on

This recipe can be found in it's original form on Stack Over Flow.