0

I'm trying to use Node.JS to provide a set of web services through restify. One of those services receives some params and then launches a process in background on Linux by using the method exec of child-process, passing as param to exec something like: "nohup program &". The process is launched without any problem, but it is killed after short interval even using nohup. I wonder if there is another way to launch a process in background through Node.JS. I've also tried to use the method spawn of child-process, but it seems that the process is not launched.

user12707
  • 294
  • 1
  • 5
  • 12
  • Possible duplicate of [Node.js workers/background processes][1]. [1]: http://stackoverflow.com/questions/4762016/node-js-workers-background-processes – Roberto Reale Apr 15 '14 at 14:33

1 Answers1

1

So both nohup and the & are not appropriate in this context. Those are utilities for when a human is using an interactive shell. When launching child processes programmatically as you are doing, you don't need them. Just launch your process by executable path/name directly. It will be in the "background" by default. The concept of "foreground" and "background" come from the notion of a single user with a single terminal screen, but remember that the OS normally runs many processes in parallel so calling something a "background" job is conceptually not quite right.

You also don't want nohup since again that is a special-purpose tool for allowing you to exit a parent shell process and leave a child process running, but in your case you want the default behavior that if your parent node.js process exits, the child worker processes should also exit.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274