开机启动Node-RED

有许多方法可以在系统启动时启动、停止和监控应用程序。本指南重点介绍了一些可能的实现方式。

树莓派, Debian, Ubuntu

我们提供的Raspberry Pi安装脚本 可以在任何类Debian操作系统上使用。

该脚本将Node-RED安装为systemd服务。 更多信息请参阅Running on Raspberry Pi 指南。

如果您未使用Raspberry Pi操作系统,可能需要根据本地用户ID和环境修改服务文件。具体操作说明可参考此处

基于RPM的Linux发行版(如RedHat、Fedora、CentOS)

我们还为基于RPM的Linux系统提供了一个安装脚本点击此处获取,该脚本还会配置systemd服务。

其他Linux、OSX系统

以下指南列出了我们认为对大多数用户来说最直接的方法。对于Windows系统,PM2不会自动作为服务运行 - 您可能更倾向于下面提到的NSSM选项

使用PM2

PM2 是一个用于Node.js的进程管理器。它可以轻松实现在系统启动时运行应用程序,并确保在必要时自动重启。

1. 安装PM2

sudo npm install -g pm2
Note : sudo is required if running as a non-root user on Linux or OS X. If running on Windows, you will need to run in a 以管理员身份运行命令提示符, without the sudo command.
If running on Windows, you should also ensure tail.exe is on your path, as described here.

2. 确定node-red命令的确切位置

如果您已全局安装了node-red,那么在Linux/OS X系统中,node-red命令通常位于:/usr/bin/node-red/usr/local/bin/node-red。可以使用which node-red命令来确认其位置。

如果是本地安装,路径将是node_modules/node-red/bin/node-red,相对于运行npm install的位置。

3. 配置PM2运行Node-RED

以下命令指示PM2运行Node-RED,假设/usr/bin/node-rednode-red命令的位置。

-- 参数必须出现在你想传递给 node-red 的任何其他参数之前。

pm2 start /usr/bin/node-red -- -v
Note : if you are running on a device like the Raspberry Pi or BeagleBone Black that have a constrained amount of memory, you must pass an additional argument:
pm2 start /usr/bin/node-red --node-args="--max-old-space-size=128" -- -v
Note : if you want to run as the root user, you must use the `--userDir` option to specify where Node-RED should store your data.

这将在后台启动Node-RED。您可以使用以下命令查看进程信息并访问日志输出:

pm2 info node-red
pm2 logs node-red

有关在PM2下管理进程的更多信息,请参阅此处

4. 配置PM2开机自启动

PM2能够生成并配置适合其运行平台的启动脚本。

运行以下命令并按照提供的指示操作:

pm2 save
pm2 startup

对于使用systemd的新版Linux系统,请使用

pm2 startup systemd
Temporary Note: There's an 打开问题 on PM2 on GitHub which highlights an issue that has been introduced recently. Linux users need to manually edit the generated `/etc/init.d/pm2-init.sh` file and replace
export PM2_HOME="/root/.pm2"
to point at the correct directory, which would be like:
export PM2_HOME="/home/{youruser}/.pm2"

5. 重启

最后,重启并检查所有内容是否按预期启动。

Windows

PM2在Windows上不会自动作为服务运行。另一种选择是使用NSSM,社区链接下方提供了一个示例。

其他选项

还有许多替代方法。以下是社区成员创建的一些方案。