控制台关闭时如何永远运行 node.js 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5818202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run node.js app forever when console is closed?
提问by Pono
I connect to my remote server via ssh. Then I start my node.js app with Forever. Everything works fine until I close my console window. How to run node.js app FOREVER on my remote server even when I close my connection via ssh? I just want to start an app and shut down my copmputer. My app should be working in the background on my remote server.
我通过 ssh 连接到我的远程服务器。然后我用Forever启动我的 node.js 应用程序。一切正常,直到我关闭控制台窗口。即使我通过 ssh 关闭连接,如何在我的远程服务器上永远运行 node.js 应用程序?我只想启动一个应用程序并关闭我的电脑。我的应用程序应该在我的远程服务器的后台运行。
采纳答案by Derrish Repchick
You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.
您可能还需要考虑使用 upstart 实用程序。它将允许您像服务一样启动、停止和重新启动节点应用程序。Upstart 可以配置为在应用程序崩溃时自动重新启动它。
Install upstart:
安装新贵:
sudo apt-get install upstart
Create a simple script for your application that will look something like:
为您的应用程序创建一个简单的脚本,如下所示:
#!upstart
description "my app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1
Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:
然后将脚本文件 (myapp.conf) 复制到 /etc/init 并确保其标记为可执行文件。然后可以使用以下命令管理您的应用程序:
sudo start myapp
sudo stop myapp
sudo restart myapp
回答by T.J. Crowder
Two answers: One for Windows, one for *nix:
两种答案:一种适用于 Windows,一种适用于 *nix:
On Windows, you can use the startcommand to start the process disconnected from your instance of cmd.exe:
在 Windows 上,您可以使用该start命令启动与您的实例断开连接的进程cmd.exe:
start node example.js
On *nix, there are two aspects of this: Disconnecting the process from the console, and making sure it doesn't receive the HUPsignal ("hang up"), which most processes (including Node) will respond to by terminating. The former is possibly optional, but the latter is necessary.
在 *nix 上,这有两个方面:断开进程与控制台的连接,并确保它没有收到HUP信号(“挂断”),大多数进程(包括 Node)将通过终止来响应。前者可能是可选的,但后者是必要的。
Starting disconnected from the console is easy: Usually, you just put an ampersand (&) at the end of the command line:
从控制台断开连接很容易:通常,您只需&在命令行末尾放置一个与号 ( ):
# Keep reading, don't just grab this and use it
node example.js &
Butthe above doesn't protect the process from HUPsignals. The program may or may not receive HUPwhen you close the shell (console), depending on a shell option called huponexit. If huponexitis true, the process will receive HUPwhen the shell exits and will presumably terminate.
但是以上并不能保护进程免受HUP信号的影响。HUP当您关闭外壳程序(控制台)时,该程序可能会也可能不会收到,具体取决于名为huponexit. 如果huponexit是true,则该进程将HUP在 shell 退出时接收并可能终止。
huponexitdefaults to falseon the various Linux variants I've used, and in fact I happily used the above for years until coderjoeand others helped me understand (in a very long comment stream under the answer that may have since been deleted) that I was relying on huponexitbeing false.
huponexit默认为false我使用过的各种 Linux 变体,事实上我很高兴地使用了上述多年,直到coderjoe和其他人帮助我理解(在可能已被删除的答案下的很长的评论流中)我依赖上huponexit是false。
To avoid the possibility that huponexitmight be truein your environment, explicitly use nohup. nohupruns the process immune from HUPsignals. You use it like this:
为避免在您的环境中huponexit出现这种可能性,true请明确使用nohup. nohup运行不受HUP信号影响的进程。你像这样使用它:
nohup node example.js > /dev/null &
or
或者
nohup node example.js > your-desired-filename-or-stream-here &
The redirection is important; if you don't do it, you'll end up with a nohup.outfile containing the output from stdoutand stderr. (By default, nohupredirects stderrto stdout, and if stdoutis outputting to a terminal, it redirects that to nohup.out. nohupalso redirects stdinif it's receiving from a terminal, so we don't have to do that. See man nohupor info coreutils 'nohup invocation'for details.)
重定向很重要;如果你不这样做,你会得到一个最终nohup.out包含从输出文件stdout和stderr。(默认情况下,nohup重定向stderr到stdout,如果stdout正在输出到终端,则将其重定向到nohup.out。如果它从终端接收,nohup也会重定向stdin,因此我们不必这样做。请参阅man nohup或info coreutils 'nohup invocation'了解详细信息。)
In general for these things, you want to use a process monitor so that if the process crashes for some reason, the monitor restarts it, but the above doeswork for simple cases.
通常,对于这些事情,你要使用过程监控,这样,如果该进程崩溃出于某种原因,该显示器将重新启动,但上面做工作简单的情况。
回答by tquang
Always, simple is the best, no need upstart, no need forever, just nohup:
永远,简单就是最好的,不需要暴发户,不需要永远,只是 nohup:
nohup node file.js &
nohup node file.js &
Believe me, I'm running so that for my case!
相信我,我正在为我的情况而跑!
回答by james murphy
I would definitely recommend pm2
npm install -g pm2
我肯定会推荐 pm2
npm install -g pm2
To start server: pm2 start [yourServerFile.js]
To stop server: pm2 stop [yourServerFile.js]
启动服务器:pm2 start [yourServerFile.js]
停止服务器:pm2 stop [yourServerFile.js]
Close client and server will run forever....will also restart if app crashes.
Ive been running a node server on Ubuntu for months with zero issues
关闭客户端和服务器将永远运行......如果应用程序崩溃也会重新启动。
我已经在 Ubuntu 上运行节点服务器几个月了,问题为零
回答by sudhir
You could install forever using npm like this:
你可以像这样使用 npm 永久安装:
sudo npm install -g forever
Or as a service:
或作为服务:
forever start server.js
Or stop service
或者停止服务
forever stop server.js
To list all running processes:
列出所有正在运行的进程:
forever list
回答by ckirksey3
回答by Emmerman
node expamle.js &for example
node expamle.js &例如
回答by Kees C. Bakker
This is only a partial answer for Windows. I've created a single line Visual Basic Script called app.vbsthat will start your node application within a hidden window:
这只是Windows的部分答案。我创建了一个名为 Visual Basic Script 的单行脚本app.vbs,它将在隐藏窗口中启动您的节点应用程序:
CreateObject("Wscript.Shell").Run "node app.js", 0
To execute it automatically at startup, open the %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\directory and add a shortcut to the app.vbsfile.
要在启动时自动执行,请打开%AppData%\Microsoft\Windows\Start Menu\Programs\Startup\目录并添加app.vbs文件的快捷方式。
More info at: https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
更多信息请访问:https: //keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
回答by Pini Cheyni
I had similar issue and I think using forever will help to handle crashed and restarts
我有类似的问题,我认为使用永远将有助于处理崩溃和重新启动
You can install forever globally:
sudo nom install -g forever
您可以在全局范围内永久安装:
sudo nom install -g forever
And run this command:
并运行此命令:
nohup forever server.js &
This should handle all the trouble of closing the terminal, closing ssh session, node crashes and restarts.
这应该处理关闭终端、关闭 ssh 会话、节点崩溃和重新启动的所有麻烦。
回答by Owen Brown
If you're running node.js in a production environment, you should consider using PM2, forever.js, or Nodemon.
如果您在生产环境中运行 node.js,则应考虑使用 PM2、forever.js 或 Nodemon。
There is no shortage of articles online comparing the different packages.
网上不乏比较不同软件包的文章。

