0

I'm trying to start nodejs server in background:

nodejs  my_app.js &

But it shows the debug messages ("Starting at 0.0.0.0 ...") and then I disconnect from my server via ssh, it terminates. Why?

Mario
  • 185
  • 9

3 Answers3

0

Try:

nohup node my_app.js &

http://www.tutorialspoint.com/unix_commands/nohup.htm

Kyll
  • 7,036
  • 7
  • 41
  • 64
Sourbh Gupta
  • 808
  • 7
  • 16
  • and still when I disconnect it terminates. – Mario Mar 02 '16 at 10:49
  • May be your server is terminates due to some error in code. you can check the logs by redirecting them into a file **nohup node my_app.js 1>./out.log 2>./error.log &** – Sourbh Gupta Mar 02 '16 at 11:44
0

Because with '&' your node process is still tied to the calling process (e.g. the Shell you typed this in). By killing your ssh-session you also kill all of its child processes.

The answer from @SourbhGupta is the one go with. nohup lets the child process ignore the hang up Signal. So, killing the calling process has no effect on its child process.

Tomas Longo
  • 95
  • 1
  • 7
0

Run it inside screen.

Screen process runs on background.

1.sudo apt-get install screen

2.screen (To start a screen process)

3.Run your service

4.node my_app.js (without & will do)

5.Ctrl+a+k to detach it.

For more information on screen- https://www.digitalocean.com/community/tutorials/how-to-install-and-use-screen-on-an-ubuntu-cloud-server

mannuscript
  • 4,711
  • 6
  • 26
  • 25