mur*_*lai 472 linux background-process node.js
我通过putty SSH连接到linux服务器.我尝试将其作为后台进程运行,如下所示:
$ node server.js &
Run Code Online (Sandbox Code Playgroud)
但是,2.5小时后终端变为非活动状态并且过程终止.无论如何,即使终端断开连接,我仍然能保持活动状态吗?
编辑1
实际上,我尝试过nohup,但只要关闭Putty SSH终端或拔掉我的互联网,服务器进程就会立即停止.
在Putty有什么我需要做的吗?
编辑2(2012年2月)
永远有一个node.js模块.它将运行node.js服务器作为守护程序服务.
小智 1105
nohup node server.js > /dev/null 2>&1 &
nohup表示:即使stty被切断,也不要终止此过程.> /dev/null表示:stdout转到/ dev/null(这是一个不记录任何输出的虚拟设备). 2>&1意思是:stderr也会转到stdout(已经重定向到/dev/null).您可以用文件路径替换&1以保留错误日志,例如:2>/tmp/myLog&最后意味着:将此命令作为后台任务运行.MK.*_*MK. 508
简单的解决方案(如果您对回到流程不感兴趣,只是希望它继续运行):
nohup node server.js &
Run Code Online (Sandbox Code Playgroud)
强大的解决方案(允许您重新连接到流程,如果它是交互式的):
screen
Run Code Online (Sandbox Code Playgroud)
然后,您可以按Ctrl + a + d分离,然后通过运行附加回来 screen -r
还要考虑更新的屏幕替代方案tmux.
Ale*_*var 137
你真的应该尝试使用screen.这有点复杂而不仅仅是做nohup long_running &,但是一旦你再也不回来理解屏幕了.
首先开始你的屏幕会话:
user@host:~$ screen
Run Code Online (Sandbox Code Playgroud)
运行你想要的任何东西
wget http://mirror.yandex.ru/centos/4.6/isos/i386/CentOS-4.6-i386-binDVD.iso
Run Code Online (Sandbox Code Playgroud)
按ctrl + A然后按d.完成.您的会话将继续在后台进行.
您可以列出所有会话screen -ls,并通过screen -r 20673.pts-0.srv命令附加到某些会话,其中0673.pts-0.srv是条目列表.
Vic*_*der 123
这是一个老问题,但在Google上排名很高.我几乎无法相信最高投票的答案,因为在屏幕会话中运行node.js进程,&甚至带有nohup标志 - 所有这些 - 都只是解决方法.
特别是screen/tmux解决方案,应该被认为是业余解决方案.Screen和Tmux并不意味着保持进程运行,而是用于多路复用终端会话.当您在服务器上运行脚本并想要断开连接时,这很好.但是对于node.js服务器,您不希望将进程附加到终端会话.这太脆弱了.为了保持运行,您需要对程序进行守护!
有很多好的工具可以做到这一点.
# basic usage
$ npm install pm2 -g
$ pm2 start server.js
# you can even define how many processes you want in cluster mode:
$ pm2 start server.js -i 4
# you can start various processes, with complex startup settings
# using an ecosystem.json file (with env variables, custom args, etc):
$ pm2 start ecosystem.json
Run Code Online (Sandbox Code Playgroud)
我看到支持PM2的一个很大的优势是它可以生成系统启动脚本,使进程在重启之间保持不变:
$ pm2 startup [platform]
Run Code Online (Sandbox Code Playgroud)
哪里platform可以ubuntu|centos|redhat|gentoo|systemd|darwin|amazon.
forever.js:https://github.com/foreverjs/forever
# basic usage
$ npm install forever -g
$ forever start app.js
# you can run from a json configuration as well, for
# more complex environments or multi-apps
$ forever start development.json
Run Code Online (Sandbox Code Playgroud)
初始化脚本:
我不会详细介绍如何编写init脚本,因为我不是这个主题的专家,这个答案太长了,但基本上它们都是简单的shell脚本,由OS事件触发.你可以在这里阅读更多相关信息
Docker:
只需使用-d选项在Docker容器中运行您的服务器,瞧,您有一个守护进程的node.js服务器!
这是一个示例Dockerfile(来自node.js 官方指南):
FROM node:argon
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
EXPOSE 8080
CMD [ "npm", "start" ]
Run Code Online (Sandbox Code Playgroud)
然后构建您的图像并运行您的容器:
$ docker build -t <your username>/node-web-app .
$ docker run -p 49160:8080 -d <your username>/node-web-app
Run Code Online (Sandbox Code Playgroud)
希望这有助于有人登陆此页面.始终使用适当的工具进行工作.它可以为您节省很多麻烦和数小时!
小智 24
另一个解决方案否认了这份工作
$ nohup node server.js &
[1] 1711
$ disown -h %1
Run Code Online (Sandbox Code Playgroud)
Dan*_*her 14
nohup即使在终端死亡后,程序也会继续运行.我实际上遇到了nohup阻止SSH会话正确终止的情况,所以你也应该重定向输入:
$ nohup node server.js </dev/null &
Run Code Online (Sandbox Code Playgroud)
根据nohup配置方式,您可能还需要将标准输出和标准错误重定向到文件.
我在我的shell rc文件中有这个功能,基于@ Yoichi的回答:
nohup-template () {
[[ "$1" = "" ]] && echo "Example usage:\nnohup-template urxvtd" && return 0
nohup "$1" > /dev/null 2>&1 &
}
Run Code Online (Sandbox Code Playgroud)
你可以这样使用它:
nohup-template "command you would execute here"
Run Code Online (Sandbox Code Playgroud)
Nohup和屏幕为后台运行Node.js提供了很好的解决方案.Node.js进程管理器(PM2)是一个方便的部署工具.在您的系统上全局安装npm:
npm install pm2 -g
运行Node.js应用程序作为守护程序:
pm2 start app.js
您可以选择将其链接到Keymetrics.io,由Unitech制作监控SAAS.
| 归档时间: |
|
| 查看次数: |
717812 次 |
| 最近记录: |