keep server alive after closing command prompt (with forever or forever-monitor)

1.5k views Asked by At

I'm using Amazon WS to test some rudimentary nodejs server. The problem I'm having is that when I close the putty command prompt on my PC, that I can not reach the server anymore with a browser.

Ive read about forever and forever-monitor. I'n not sure why the script must be restarted constantly, but ok let's assume it must.

I'm using both

forever "/home/ec2-user/myApp.js"

and

node "/home/ec2-user/foreverMonitor.js"

(The latter has the myApp.js reference in the foreverMonitor.js file. Similar to Where place forever-monitor code?.)

Both do start the server, but when I close putty, both also let the server die.

What am I missing here?

------------------------------------- update -------------------------------------

I guess I can also skip foreverMonitor (not verified yet)

nohup forever "/home/ec2-user/myApp.js" &
forever stop "/home/ec2-user/myApp.js"

------------------------------------- update -------------------------------------

working, now using this

nohup forever "/home/ec2-user/foreverMonitor.js" &
forever stop "/home/ec2-user/foreverMonitor.js"
2

There are 2 answers

0
CatDadCode On BEST ANSWER

I'm not totally familiar with AWS, but it seems that you probably need to run nohup. The trailing ampersand should give you control of the terminal again immediately after executing the command.

$ nohup forever "/home/ec2-user/myApp.js" &
$ nohup node "/home/ec2-user/foreverMonitor.js" &

See this answer for more details on nohup and the trailing ampersand: https://stackoverflow.com/a/15595391/498624

0
RoyHB On

Have a look at PM2 https://github.com/Unitech/pm2

After using forever successfully, I switched to PM2.

forever works fine but I found PM2 was a better fit to my mental model. PM2 also has a very neat (and repidly evolving) Web interface where you can monitor and control node instances. As a bonus you can also run non-node tasks under PM2