由于多年来这篇文章引起了很多关注,我在这篇文章的底部列出了每个平台的顶级解决方案。
原帖:
我希望我的node.js 服务器在后台运行,即:当我关闭终端时,我希望我的服务器继续运行。我用谷歌搜索了这个并想出了这个教程,但是它不起作用正如预期的那样。因此,我没有使用该守护程序脚本,而是认为我只是使用了输出重定向(2>&1>>文件
部分),但这也不会退出 - 我得到一个空行在我的终端中,就像它正在等待输出/错误一样。
我还尝试将进程置于后台,但是一旦我关闭终端,该进程也会被终止。
那么当我关闭本地计算机时如何让它继续运行呢?
顶级解决方案:
Since this post has gotten a lot of attention over the years, I've listed the top solutions per platform at the bottom of this post.
Original post:
I want my node.js server to run in the background, i.e.: when I close my terminal I want my server to keep running. I've googled this and came up with this tutorial, however it doesn't work as intended. So instead of using that daemon script, I thought I just used the output redirection (the 2>&1 >> file
part), but this too does not exit - I get a blank line in my terminal, like it's waiting for output/errors.
I've also tried to put the process in the background, but as soon as I close my terminal the process is killed as well.
So how can I leave it running when I shut down my local computer?
Top solutions:
发布评论
评论(30)
从 复制我自己的答案我应该将 Node.js 应用程序作为自己的进程运行吗?
2015 年答案:几乎每个 Linux 发行版都附带 systemd,这意味着 forever、monit、PM2 等不再是必要的 - 您的操作系统已经处理这些任务。
创建一个
myapp.service
文件(显然,将“myapp”替换为您的应用程序名称):如果您是 Unix 新手,请注意:
/var/www/ myapp/app.js
应该在第一行有#!/usr/bin/env 节点
并打开可执行模式chmod +x app.js.
将您的服务文件复制到
/etc/systemd/system
中。使用
systemctl start myapp
启动它。使用
systemctl enable myapp
使其能够在启动时运行。使用
journalctl -u myapp
查看日志这取自我们如何在 Linux 2018 版上部署节点应用程序,其中还包括生成 AWS/DigitalOcean/Azure CloudConfig 来构建 Linux/ 的命令/节点服务器(包括
.service
文件)。Copying my own answer from How do I run a Node.js application as its own process?
2015 answer: nearly every Linux distro comes with systemd, which means forever, monit, PM2, etc are no longer necessary - your OS already handles these tasks.
Make a
myapp.service
file (replacing 'myapp' with your app's name, obviously):Note if you're new to Unix:
/var/www/myapp/app.js
should have#!/usr/bin/env node
on the very first line and have the executable mode turned onchmod +x app.js
.Copy your service file into the
/etc/systemd/system
.Start it with
systemctl start myapp
.Enable it to run on boot with
systemctl enable myapp
.See logs with
journalctl -u myapp
This is taken from How we deploy node apps on Linux, 2018 edition, which also includes commands to generate an AWS/DigitalOcean/Azure CloudConfig to build Linux/node servers (including the
.service
file).更新 - 正如下面的一个答案中提到的,PM2 有一些非常好的功能永远缺失。考虑使用它。
原始答案
使用nohup:
编辑我想补充一点,接受的答案确实是要走的路。我在需要保持运行状态的实例上使用forever。我喜欢执行
npm install -g permanent
因此它位于节点路径中,然后执行forever start server.js
UPDATE - As mentioned in one of the answers below, PM2 has some really nice functionality missing from forever. Consider using it.
Original Answer
Use nohup:
EDIT I wanted to add that the accepted answer is really the way to go. I'm using forever on instances that need to stay up. I like to do
npm install -g forever
so it's in the node path and then just doforever start server.js
您可以使用 Forever,这是一个简单的 CLI 工具,用于确保给定的节点脚本连续运行(即永远):
https://www.npmjs.org/package/forever
You can use Forever, A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever):
https://www.npmjs.org/package/forever
这可能不是公认的方式,但我用屏幕来做到这一点,特别是在开发过程中,因为我可以在必要时将其恢复并愚弄它。
屏幕将分离并在您注销后继续存在。然后你可以通过 screen -r 将其恢复。点击屏幕手册了解更多详细信息。如果您愿意,您可以为屏幕命名等等。
This might not be the accepted way, but I do it with screen, especially while in development because I can bring it back up and fool with it if necessary.
The screen will detach and survive you logging off. Then you can get it back back doing screen -r. Hit up the screen manual for more details. You can name the screens and whatnot if you like.
2016 年更新:
node-windows/mac/linux系列使用跨所有操作系统的通用API,因此它绝对是一个相关的解决方案。然而; node-linux 生成 systemv 初始化文件。随着 systemd 的不断普及,它实际上是 Linux 上更好的选择。如果有人想为 node-linux 添加 systemd 支持,欢迎 PR :-)
原始线程:
这是一个非常旧的线程,但是 node-windows 提供了另一种在 Windows 上创建后台服务的方法。它大致基于在节点脚本周围使用
exe
包装器的nssm
概念。然而;它使用winsw.exe
来代替,并提供一个可配置的节点包装器,以便更精细地控制进程在失败时如何启动/停止。这些进程与任何其他服务一样可用:该模块还包含一些事件日志记录:
守护进程化脚本是通过代码完成的。例如:
该模块支持诸如限制重新启动(因此糟糕的脚本不会破坏您的服务器)和增加重新启动之间的时间间隔等功能。
由于节点窗口服务与其他服务一样运行,因此可以使用您已经使用的任何软件来管理/监视服务。
最后,没有
make
依赖项。换句话说,简单的npm install -g node-windows
就可以了。您不需要 Visual Studio、.NET 或 node-gyp magic 来安装它。此外,它还获得了 MIT 和 BSD 许可。坦白说,我是这个模块的作者。它旨在减轻 OP 所经历的确切痛苦,但更紧密地集成到操作系统已提供的功能中。我希望未来有同样问题的观众会发现它很有用。
2016 Update:
The node-windows/mac/linux series uses a common API across all operating systems, so it is absolutely a relevant solution. However; node-linux generates systemv init files. As systemd continues to grow in popularity, it is realistically a better option on Linux. PR's welcome if anyone wants to add systemd support to node-linux :-)
Original Thread:
This is a pretty old thread now, but node-windows provides another way to create background services on Windows. It is loosely based on the
nssm
concept of using anexe
wrapper around your node script. However; it useswinsw.exe
instead and provides a configurable node wrapper for more granular control over how the process starts/stops on failures. These processes are available like any other service:The module also bakes in some event logging:
Daemonizing your script is accomplished through code. For example:
The module supports things like capping restarts (so bad scripts don't hose your server) and growing time intervals between restarts.
Since node-windows services run like any other, it is possible to manage/monitor the service with whatever software you already use.
Finally, there are no
make
dependencies. In other words, a straightforwardnpm install -g node-windows
will work. You don't need Visual Studio, .NET, or node-gyp magic to install this. Also, it's MIT and BSD licensed.In full disclosure, I'm the author of this module. It was designed to relieve the exact pain the OP experienced, but with tighter integration into the functionality the Operating System already provides. I hope future viewers with this same question find it useful.
如果您只是想不间断地运行脚本直到完成,您可以使用
nohup
(如此处答案中已提到的)。但是,没有一个答案提供同时记录 stdin 和 stdout 的完整命令。>>
表示附加到app.log
。2>&1
确保错误也发送到stdout
并添加到app.log
中。&
确保您当前的终端与命令断开连接,以便您可以继续工作。如果您想运行节点服务器(或者在服务器重新启动时应该启动备份的东西),您应该使用 systemd / systemctl。
If you simply want to run the script uninterrupted until it completes you can use
nohup
as already mentioned in the answers here. However, none of the answers provide a full command that also logsstdin
andstdout
.>>
means append toapp.log
.2>&1
makes sure that errors are also send tostdout
and added to theapp.log
.&
makes sure your current terminal is disconnected from command so you can continue working.If you want to run a node server (or something that should start back up when the server restarts) you should use systemd / systemctl.
更新:我更新了 pm2 的最新内容:
对于许多用例,使用 systemd 服务是管理节点进程的最简单、最合适的方法。对于那些在单一环境中运行大量节点进程或独立运行的节点微服务的人来说,pm2 是一个功能更齐全的工具。
https://github.com/unitech/pm2
http://pm2.io
pm2 monit
命令行监控多个进程,或使用pm2 list
pm2 记录
<块引用>
UPDATE: i updated to include the latest from pm2:
for many use cases, using a systemd service is the simplest and most appropriate way to manage a node process. for those that are running numerous node processes or independently-running node microservices in a single environment, pm2 is a more full featured tool.
https://github.com/unitech/pm2
http://pm2.io
pm2 monit
or process list withpm2 list
pm2 logs
如果您正在使用nohup,请尝试运行此命令 -
您也可以使用forever来启动服务器
PM2也支持
npm start
Try to run this command if you are using nohup -
You can also use forever to start server
PM2 also supports
npm start
如果您运行的是 OSX,那么生成真正的系统进程的最简单方法是使用
launchd
来启动它。构建一个像这样的 plist,并将其放入 /Library/LaunchDaemons 中,名称为
top-level-domain.your-domain.application.plist
(放置时您需要是 root):完成后,发出此命令(以 root 身份):
然后您就可以运行了。
重新启动后您仍将运行。
有关 plist 中的其他选项,请查看此处的手册页: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html
If you are running OSX, then the easiest way to produce a true system process is to use
launchd
to launch it.Build a plist like this, and put it into the /Library/LaunchDaemons with the name
top-level-domain.your-domain.application.plist
(you need to be root when placing it):When done, issue this (as root):
and you are running.
And you will still be running after a restart.
For other options in the plist look at the man page here: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html
我只是使用 daemon npm 模块:
最近我也在使用 mon(1)< /a> 来自 TJ Holowaychuk 用于启动和管理简单的节点应用程序。
I am simply using the daemon npm module:
Lately I'm also using mon(1) from TJ Holowaychuk to start and manage simple node apps.
我使用 Supervisor 进行开发。它就是有效的。当您对 .js 文件进行更改时,Supervisor 会自动重新启动您的应用程序并加载这些更改。
这是其 Github 页面的链接
安装:
您可以使用 -e 轻松使其监视其他扩展。我经常使用的另一个命令是 -i 来忽略某些文件夹。
您可以使用 nohup 和 Supervisor 使您的节点应用程序在后台运行,即使在您注销后也是如此。
I use Supervisor for development. It just works. When ever you make changes to a .js file Supervisor automatically restarts your app with those changes loaded.
Here's a link to its Github page
Install :
You can easily make it watch other extensions with -e. Another command I use often is -i to ignore certain folders.
You can use nohup and supervisor to make your node app run in the background even after you log out.
Node.js 作为 WINDOWS XP 中的后台服务
安装:
创建c:\node\helloworld.js
打开命令控制台并键入以下内容(仅当安装了资源工具包时才使用 setx)
一个漂亮的批处理好东西是创建 c:\node\ServiceMe.cmd
服务管理:
services.msc 或通过“开始”->“运行”->“ MSCONFIG->服务(并选中“隐藏
所有 Microsoft 服务')。
'节点-'。
Node.js as a background service in WINDOWS XP
Installation:
Create c:\node\helloworld.js
Open command console and type the following (setx only if Resource Kit is installed)
A nifty batch goodie is to create c:\node\ServiceMe.cmd
Service Management:
services.msc or via Start->Run-> MSCONFIG-> Services (and check 'Hide
All Microsoft Services').
'node-'.
接受的答案可能是最好的生产答案,但是对于进行开发工作的快速破解,我发现:
nodejs scriptname.js &
不起作用,因为 nodejs 似乎吞噬了 & ,所以这件事不允许我继续使用终端而不导致 scriptname.js 死亡。但我将
nodejs scriptname.js
放在 .sh 文件中,并且nohup sh startscriptname.sh &
有效。绝对不是生产问题,但它解决了“我需要继续使用我的终端并且不想启动 5 个不同的终端”问题。
The accepted answer is probably the best production answer, but for a quick hack doing dev work, I found this:
nodejs scriptname.js &
didn't work, because nodejs seemed to gobble up the &, and so the thing didn't let me keep using the terminal without scriptname.js dying.But I put
nodejs scriptname.js
in a .sh file, andnohup sh startscriptname.sh &
worked.Definitely not a production thing, but it solves the "I need to keep using my terminal and don't want to start 5 different terminals" problem.
2017 年 6 月更新:
Linux 的解决方案:(红帽)。以前的评论对我不起作用。
这对我在 Amazon Web Service - Red Hat 7 上有用。希望这对其他人有用。
June 2017 Update:
Solution for Linux: (Red hat). Previous comments doesn't work for me.
This works for me on Amazon Web Service - Red Hat 7. Hope this works for somebody out there.
如果你在linux服务器上运行nodejs,我认为这是最好的方法。
创建服务脚本并复制到 /etc/init/nodejs.conf
启动服务: sudo service nodejs start
停止服务: sudo service nodejs stop
服务脚本
If you are running nodejs in linux server, I think this is the best way.
Create a service script and copy to /etc/init/nodejs.conf
start service: sudo service nodejs start
stop service: sudo service nodejs stop
Sevice script
我有点晚了。简单的解决方案是在终端中执行以下两个命令,如下所示。
然后关闭 ssh 会话,节点程序继续运行。在 Ubuntu 18 上测试。
I am bit late. Simple solution is to execute below two commands in terminal as shown below.
Then close your ssh session and node program continues to run. Tested on Ubuntu 18.
使用 nssm Windows 最佳解决方案,只需下载 nssm,打开 cmd 到 nssm 目录并键入
此命令将安装一个新的 Windows 服务,该服务将在 services.msc 中列出,您可以从那里启动或停止该服务,该服务将自动启动,您可以配置在失败时重新启动。
use nssm the best solution for windows, just download nssm, open cmd to nssm directory and type
this will install a new windows service which will be listed at services.msc from there you can start or stop the service, this service will auto start and you can configure to restart if it fails.
使用pm2模块。 pm2 Nodejs 模块
Use pm2 module. pm2 nodejs module
由于我在提供的答案列表中缺少此选项,因此我想在 2020 年添加一个符合条件的选项: docker 或任何同等内容 容器平台。除了确保您的应用程序在稳定的环境中运行之外,还有其他安全优势以及改进的可移植性。
Windows、macOS 和大多数/主要 Linux 发行版都支持 docker。 在受支持的平台上安装 docker 相当简单且有详细记录。设置 Node.js 应用程序非常简单,只需将其放入容器中并运行该容器,同时确保其在关闭后重新启动。
创建容器映像
假设您的应用程序在该服务器上的 /home/me/my-app 中可用,请创建一个文本文件 Dockerfile 位于文件夹 /home/me 中,内容与此类似:
它正在创建一个用于运行的映像Alpine Linux 下 Node.js 的 LTS 版本,将应用程序的文件复制到映像中并运行 npm ci 以确保依赖项与运行时上下文匹配。
在包含内容的同一文件夹中创建另一个文件 .dockerignore
这将防止主机系统的现有依赖项注入到容器中,因为它们可能无法在容器中工作。 Dockerfile 中提供的
RUN
命令将解决这个问题。使用如下命令创建镜像:
-t
选项选择构建容器镜像的“名称”。这用于下面运行的容器。注意:最后一个参数是选择包含该 Dockerfile 的文件夹而不是 Dockerfile 本身。您可以使用选项
-f
选择不同的选项。启动容器
使用此命令启动容器:
此命令假设您的应用程序正在侦听端口 3000,并且您希望它在主机的端口 80 上公开。
这当然是一个非常有限的例子,但它是一个很好的起点。
Since I'm missing this option in the list of provided answers I'd like to add an eligible option as of 2020: docker or any equivalent container platform. In addition to ensuring your application is working in a stable environment there are additional security benefits as well as improved portability.
There is docker support for Windows, macOS and most/major Linux distributions. Installing docker on a supported platform is rather straight-forward and well-documented. Setting up a Node.js application is as simple as putting it in a container and running that container while making sure its being restarted after shutdown.
Create Container Image
Assuming your application is available in /home/me/my-app on that server, create a text file Dockerfile in folder /home/me with content similar to this one:
It is creating an image for running LTS version of Node.js under Alpine Linux, copying the application's files into the image and runs
npm ci
to make sure dependencies are matching that runtime context.Create another file .dockerignore in same folder with content
This will prevent existing dependencies of your host system from being injected into container as they might not work there. The presented
RUN
command in Dockerfile is going to fix that.Create the image using command like this:
The
-t
option is selecting the "name" of built container image. This is used on running containers below.Note: Last parameter is selecting folder containing that Dockerfile instead of the Dockerfile itself. You may pick a different one using option
-f
.Start Container
Use this command for starting the container:
This command is assuming your app is listening on port 3000 and you want it to be exposed on port 80 of your host.
This is a very limited example for sure, but it's a good starting point.
为了完善建议的各种选项,这里还有一个:GNU/Linux 中的
daemon
命令,您可以在此处阅读:http://libslack.org/daemon/manpages/daemon.1.html。 (如果上面的评论之一已经提到了这一点,我们深表歉意)。To round out the various options suggested, here is one more: the
daemon
command in GNU/Linux, which you can read about here: http://libslack.org/daemon/manpages/daemon.1.html. (apologies if this is already mentioned in one of the comments above).看看赋格!除了启动许多工作人员之外,您还可以妖魔化您的节点进程!
http://github.com/pgte/fugue
Check out fugue! Apart from launching many workers, you can demonize your node process too!
http://github.com/pgte/fugue
PM2 是 Node.js 应用程序的生产流程管理器,具有内置负载均衡器。它允许您使应用程序永远保持活动状态,无需停机即可重新加载它们,并简化常见的系统管理任务。
https://github.com/Unitech/pm2
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
https://github.com/Unitech/pm2
我很惊讶没有人提到 Guvnor
我一直在尝试,pm2 等。但是,当它出现时对于可靠的控制和基于网络的性能指标,我发现 Guvnor 是迄今为止最好的。另外,它也是完全开源的。
编辑:但是,我不确定它是否适用于 Windows。我只在 Linux 上使用过它。
I am surprised that nobody has mentioned Guvnor
I have tried forever, pm2, etc. But, when it comes to solid control and web based performance metrics, I have found Guvnor to be by far the best. Plus, it is also fully opensource.
Edit : However, I am not sure if it works on windows. I've only used it on linux.
有人注意到“2>&1”的位置有一个小错误吗?
应该是
has anyone noticed a trivial mistaken of the position of "2>&1" ?
should be
我将 tmux 用于远程主机上的多窗口/窗格开发环境。分离并保持进程在后台运行非常简单。看看 tmux
I use tmux for a multiple window/pane development environment on remote hosts. It's really simple to detach and keep the process running in the background. Have a look at tmux
对于使用较新版本的 daemon npm 模块的人 - 您需要传递文件描述符而不是字符串:
For people using newer versions of the daemon npm module - you need to pass file descriptors instead of strings:
如果您使用 pm2,则可以将
autorestart
设置为false
来使用它:这将生成一个示例
ecosystem.config.js
:If you are using pm2, you can use it with
autorestart
set tofalse
:This will generate a sample
ecosystem.config.js
:我在 RHEL 8 AWS EC2 实例上使用 @mikemaccana 接受的答案时收到以下错误:
(code =exited, status=216/GROUP)
这是由于使用的用户/组设置为:“nobody”。
经过谷歌搜索,似乎使用用户/组作为“nobody”/“nogroup”对于守护进程来说是不好的做法,正如所回答的此处位于 unix 堆栈交换上。
将用户/组设置为我的实际用户和组后,效果非常好。
您可以输入
whomai
和groups
以查看解决此问题的可用选项。我的带有 mongodb 的完整堆栈节点应用程序的服务文件:
I received the following error when using @mikemaccana's accepted answer on a RHEL 8 AWS EC2 instance:
(code=exited, status=216/GROUP)
It was due to using the user/group set to: 'nobody'.
Upon googling, it seems that using user/group as 'nobody'/'nogroup' is bad practice for daemons as answered here on the unix stack exchange.
It worked great after I set user/group to my actual user and group.
You can enter
whomai
andgroups
to see your available options to fix this.My service file for a full stack node app with mongodb:
您可以在 unix 中使用
&
在后台运行节点应用程序。确保您的节点应用程序不应该有任何交互模式,例如从输入接收数据。You can run your node app in background with
&
in unix. Make sure your node app should not have any interactive mode like receive data from input.这个答案已经很晚了,但我发现最好的解决方案是编写一个同时使用
screen -dmS
和nohup
命令的 shell 脚本。我还添加了
>> logfile
位位于末尾,这样我就可以轻松保存节点console.log()
语句。为什么我使用 shell 脚本?我还添加了一个 if 语句来检查
node myserver.js
进程是否已经在运行。这样我就能够创建一个命令行选项,既可以让我保持服务器运行,也可以在我进行更改时重新启动它,这对开发非常有帮助。
This answer is quite late to the party, but I found that the best solution was to write a shell script that used the both the
screen -dmS
andnohup
commands.I also add the
>> logfile
bit on the end so I can easily save the nodeconsole.log()
statements.Why did I use a shell script? Well I also added in an if statement that checked to see if the
node myserver.js
process was already running.That way I was able to create a single command line option that both lets me keep the server going and also restart it when I have made changes, which is very helpful for development.