How to run Node.js as a background process and nev

2018-12-31 10:16发布

I connect to the linux server via putty SSH. I tried to run it as a background process like this:

$ node server.js &

However, after 2.5 hrs the terminal becomes inactive and the process dies. Is there anyway I can keep the process alive even with the terminal disconnected?


Edit 1

Actually, I tried nohup, but as soon as I close the Putty SSH terminal or unplug my internet, the server process stops right away.

Is there anything I have to do in Putty?


Edit 2 (on Feb, 2012)

There is a node.js module, forever. It will run node.js server as daemon service.

14条回答
明月照影归
2楼-- · 2018-12-31 10:48

Nohup and screen offer great light solutions to running Node.js in the background. Node.js process manager (PM2) is a handy tool for deployment. Install it with npm globally on your system:

npm install pm2 -g

to run a Node.js app as a daemon:

pm2 start app.js

You can optionally link it to Keymetrics.io a monitoring SAAS made by Unitech.

查看更多
闭嘴吧你
3楼-- · 2018-12-31 10:49

For Ubuntu i use this:

(exec PROG_SH &> /dev/null &)

regards

查看更多
其实,你不懂
4楼-- · 2018-12-31 10:57

another solution disown the job

$ nohup node server.js &
[1] 1711
$ disown -h %1
查看更多
骚的不知所云
5楼-- · 2018-12-31 11:00

Apart from cool solutions above I'd mention also about supervisord and monit tools which allow to start process, monitor its presence and start it if it died. With 'monit' you can also run some active checks like check if process responds for http request

查看更多
栀子花@的思念
6楼-- · 2018-12-31 11:01

Try this for a simple solution

cmd & exit

查看更多
姐姐魅力值爆表
7楼-- · 2018-12-31 11:04
$ disown node server.js &

It will remove command from active task list and send the command to background

查看更多
登录 后发表回答