由于这篇文章在过去几年里受到了很多关注,所以我在文章的底部列出了每个平台的最佳解决方案。


原来的帖子:

我希望我的node.js服务器在后台运行,即:当我关闭我的终端时,我希望我的服务器继续运行。我在谷歌上搜索过这个,并提出了这个教程,但它并不像预期的那样工作。因此,我没有使用这个守护进程脚本,我认为我只是使用了输出重定向(2>&1 >>文件部分),但这也不退出-我在终端中得到一个空行,就像它在等待输出/错误一样。

我也尝试过把进程放在后台,但是只要我关闭终端,进程就会被杀死。

那么,当我关闭本地计算机时,如何让它继续运行呢?


高级解决方案:

Systemd (Linux) Launchd (Mac) node-windows (Windows) PM2 (node . js)


当前回答

Node.js作为WINDOWS XP的后台服务

Hacksparrow在http://www.hacksparrow.com/install-node-js-and-npm-on-windows.html上提供了在windows下安装Node.js + npm的教程。 为了nnsm.exe的实现,荣誉归于Tatham Oddie: http://blog.tatham.oddie.com.au/2011/03/16/node-js-on-windows/。

安装:

Install WGET http://gnuwin32.sourceforge.net/packages/wget.htm via installer executable Install GIT http://code.google.com/p/msysgit/downloads/list via installer executable Install NSSM http://nssm.cc/download/?page=download via copying nnsm.exe into %windir%/system32 folder Create c:\node\helloworld.js // http://howtonode.org/hello-node var http = require('http'); var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World\n"); }); server.listen(8000); console.log("Server running at http://127.0.0.1:8000/"); Open command console and type the following (setx only if Resource Kit is installed) C:\node> set path=%PATH%;%CD% C:\node> setx path "%PATH%" C:\node> set NODE_PATH="C:\Program Files\nodejs\node_modules" C:\node> git config --system http.sslcainfo /bin/curl-ca-bundle.crt C:\node> git clone --recursive git://github.com/isaacs/npm.git C:\node> cd npm C:\node\npm> node cli.js install npm -gf C:\node> cd .. C:\node> nssm.exe install node-helloworld "C:\Program Files\nodejs\node.exe" c:\node\helloworld.js C:\node> net start node-helloworld A nifty batch goodie is to create c:\node\ServiceMe.cmd @echo off nssm.exe install node-%~n1 "C:\Program Files\nodejs\node.exe" %~s1 net start node-%~n1 pause

服务管理:

服务本身现在可以通过Start-> Run->访问 服务。msc或通过Start->运行-> MSCONFIG-> Services(并检查“隐藏” 所有微软服务)。 该脚本将为通过批处理脚本生成的每个节点加上前缀 “节点——”。 同样,它们可以在注册表中找到:"HKLM\SYSTEM\CurrentControlSet\Services\node-xxxx"

其他回答

如果出于本地开发的目的,你需要启动多个NodeJS应用实例(express, fastify等),那么并发可能是一个选项。下面是一个设置:

先决条件

你的NodeJS应用(express, fastify等)被放置在/opt/mca/www/mca-backend/app路径下。 假设您使用的是通过brew install node@16安装的节点v.16

设置

Install concurrently: npm install -g concurrently Create a file ~/Library/LaunchAgents/mca.backend.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>mca.backend</string> <key>LimitLoadToSessionType</key> <array> <string>Aqua</string> <string>Background</string> <string>LoginWindow</string> <string>StandardIO</string> <string>System</string> </array> <key>ProgramArguments</key> <array> <string>/usr/local/bin/concurrently</string> <string>--names</string> <string>dev,prd</string> <string>--success</string> <string>all</string> <string>--kill-others</string> <string>--no-color</string> <string>MCA_APP_STAGE=dev node ./server.mjs</string> <string>MCA_APP_STAGE=prod node ./server.mjs</string> </array> <key>RunAtLoad</key> <true/> <key>EnvironmentVariables</key> <dict> <key>PATH</key> <string>/usr/local/opt/node@16/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string> </dict> <key>WorkingDirectory</key> <string>/opt/mca/www/mca-backend/app</string> <key>StandardErrorPath</key> <string>/opt/mca/www/mca-backend/err.log</string> <key>StandardOutPath</key> <string>/opt/mca/www/mca-backend/out.log</string> </dict> </plist> Load and run: launchctl bootstrap gui/`id -u` $HOME/Library/LaunchAgents/mca.backend.plist Get a status: launchctl print gui/`id -u`/mca.backend Stop: launchctl kill SIGTERM gui/`id -u`/mca.backend Start/Restart: launchctl kickstart -k -p gui/`id -u`/mca.backend Unload if not needed anymore: launchctl bootout gui/`id -u`/mca.backend

重要提示:一旦你用launchctl加载service,引导你在文件中所做的任何更改 ~ /图书馆/ LaunchAgents / mca.backend。Plist将不会在行动,直到你卸载服务(通过使用launchctl bootout) 然后再次加载它(通过使用launchctl bootstrap)。

故障排除

查看启动日志:/private/var/log/com.apple.xpc.launchd/launchd.log

我在远程主机上使用tmux作为多窗口/窗格开发环境。分离并保持进程在后台运行非常简单。看看tmux

有人注意到“2>&1”的位置有一个小错误吗?

2>&1 >> file

应该是

>> file 2>&1

由于我在提供的答案列表中缺少这个选项,我想在2020年添加一个合格的选项:docker或任何等效的容器平台。除了确保您的应用程序在稳定的环境中工作外,还有额外的安全性好处以及改进的可移植性。

docker支持Windows, macOS和大多数/主要的Linux发行版。在一个受支持的平台上安装docker相当简单,并且有良好的文档。设置Node.js应用程序非常简单,只需将其放入容器中并运行该容器,同时确保在关闭后重新启动。

创建容器镜像

假设你的应用程序在服务器上的/home/me/my-app中可用,在/home/me文件夹中创建一个文本文件Dockerfile,内容类似于下面这个:

FROM node:lts-alpine
COPY /my-app/ /app/
RUN cd /app && npm ci
CMD ["/app/server.js"]

它为在Alpine Linux下运行LTS版本的Node.js创建了一个映像,将应用程序的文件复制到映像中,并运行npm ci以确保依赖项与运行时上下文匹配。

在相同的文件夹中创建另一个文件。dockerignore

**/node_modules

这将防止宿主系统的现有依赖项被注入到容器中,因为它们可能在那里不起作用。Dockerfile中的RUN命令将修复这个问题。

使用如下命令创建图像:

docker build -t myapp-as-a-service /home/me

-t选项选择构建的容器映像的“名称”。这将用于下面运行的容器。

注意:最后一个参数是选择包含Dockerfile的文件夹,而不是Dockerfile本身。你可以使用选项-f选择一个不同的选项。

启动容器

使用此命令启动容器:

docker run -d --restart always -p 80:3000 myapp-as-a-service

这个命令假设你的应用程序正在监听端口3000,而你想让它暴露在主机的端口80上。

当然,这是一个非常有限的例子,但它是一个很好的起点。

如果您只是想不间断地运行脚本,直到它完成,您可以使用nohup,就像这里的答案中提到的那样。但是,没有一个答案提供一个完整的命令来记录stdin和stdout。

nohup node index.js >> app.log 2>&1 &

>>表示追加到app.log。 2>&1确保错误也被发送到标准输出并添加到app.log中。 结束&确保您当前的终端与命令断开连接,以便您可以继续工作。

如果你想运行一个节点服务器(或者在服务器重新启动时重新启动),你应该使用systemd / systemctl。