如何将 Node.js 作为后台进程运行并且永不消亡? [英] How to run Node.js as a background process and never die?
问题描述
我通过 putty SSH 连接到 linux 服务器.我尝试将它作为这样的后台进程运行:
I connect to the linux server via putty SSH. I tried to run it as a background process like this:
$ node server.js &
但是,2.5 小时后终端变为非活动状态并且进程终止.无论如何,即使终端断开连接,我也可以保持进程存活吗?
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?
编辑 1
实际上,我尝试了 nohup
,但是只要我关闭 Putty SSH 终端或拔下互联网,服务器进程就会立即停止.
Actually, I tried nohup
, but as soon as I close the Putty SSH terminal or unplug my internet, the server process stops right away.
我需要在 Putty 中做些什么吗?
Is there anything I have to do in Putty?
编辑 2(2012 年 2 月)
有一个 node.js
模块,forever.它将 node.js 服务器作为守护程序服务运行.
There is a node.js
module, forever. It will run node.js server as daemon service.
推荐答案
简单的解决方案(如果你对返回进程不感兴趣,只希望它继续运行):
Simple solution (if you are not interested in coming back to the process, just want it to keep running):
nohup node server.js &
还有 jobs
命令可以查看这些后台进程的索引列表.您可以通过运行 kill %1
或 kill %2
来终止后台进程,数字是进程的索引.
There's also the jobs
command to see an indexed list of those backgrounded processes. And you can kill a backgrounded process by running kill %1
or kill %2
with the number being the index of the process.
强大的解决方案(允许您重新连接到交互过程):
Powerful solution (allows you to reconnect to the process if it is interactive):
screen
然后您可以按 Ctrl+a+d 分离,然后通过运行 screen -r
You can then detach by pressing Ctrl+a+d and then attach back by running screen -r
还要考虑屏幕的更新替代品 tmux.
Also consider the newer alternative to screen, tmux.
这篇关于如何将 Node.js 作为后台进程运行并且永不消亡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!