Persistent applications: Startup Script Generator持久应用程序:启动脚本生成器
PM2 can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts.PM2可以生成启动脚本并对其进行配置,以便在预期或意外的机器重启过程中保持流程列表的完整性。
Generating a Startup Script生成启动脚本
To automatically generate and configuration a startup script just type the command (without sudo) 要自动生成和配置启动脚本,只需键入命令(不带sudo)pm2 startup
:pm2 startup
:
$ pm2 startup
[PM2] You have to run this command as root. Execute the following command:
sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v14.3/bin pm2 startup <distribution> -u <user> --hp <home-path>
Then copy/paste the displayed command onto the terminal:然后将显示的命令复制/粘贴到终端上:
sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v14.3/bin pm2 startup <distribution> -u <user> --hp <home-path>
Now PM2 will automatically restart at boot.现在PM2将在开机时自动重启。
Note: You can customize the service name via the 注意:您可以通过--service-name <name>
option (#3213)--service-name <name>
选项(#3213)
Saving the app list to be restored at reboot保存要在重新启动时还原的应用程序列表
Once you have started all desired apps, save the app list so it will respawn after reboot:启动所有所需的应用程序后,保存应用程序列表,以便在重新启动后重新启动:
pm2 save
Manually resurrect processes手动恢复进程
To manually bring back previously saved processes (via pm2 save):要手动恢复以前保存的流程(通过pm2保存):
pm2 resurrect
Disabling startup system禁用启动系统
To disable and remove the current startup configuration:要禁用和删除当前启动配置,请执行以下操作:
pm2 unstartup
Specifics to know要知道的细节
Updating startup script after Node.js version upgradeNode.js版本升级后更新启动脚本
When you upgrade the local Node.js version, be sure to update the PM2 startup script, so it runs the latest Node.js binary you have installed.升级本地Node.js版本时,请确保更新PM2启动脚本,使其运行您安装的最新Node.js二进制文件。
First disable and remove the current startup configuration (copy/paste the output of that command):首先禁用并删除当前启动配置(复制/粘贴该命令的输出):
$ pm2 unstartup
Then restore a fresh startup script:然后还原一个新的启动脚本:
$ pm2 startup
User permissions用户权限
Let’s say you want the startup script to be executed under another user.假设您希望启动脚本在另一个用户下执行。
Just change the 只需更改-u <username>
option and the --hp <user_home>
:-u <username>
选项和--hp <user_home>
选项:
pm2 startup ubuntu -u www --hp /home/ubuntu
Specifying the init system指定init系统
You can specify the platform you use by yourself if you want to (where platform can be either one of the cited above):如果愿意,您可以指定自己使用的平台(其中平台可以是上述任一平台):
pm2 startup [ubuntu | ubuntu14 | ubuntu12 | centos | centos6 | arch | oracle | amazon | macos | darwin | freebsd | systemd | systemv | upstart | launchd | rcd | openrc]
SystemD installation checking系统安装检查
# Check if pm2-<USER> service has been added检查pm2-<USER>是否已添加服务
$ systemctl list-units
# Check logs检查日志
$ journalctl -u pm2-<USER>
# Cat systemd configuration fileCat systemd配置文件
$ systemctl cat pm2-<USER>
# Analyze startup分析启动
$ systemd-analyze plot > output.svg
To wait efficiently that machine is online for PM2 to run:要高效地等待机器联机以运行PM2,请执行以下操作:
[Unit]
Wants=network-online.target
After=network.target network-online.target
[....]
[Install]
WantedBy=multi-user.target network-online.target
Windows startup scriptWindows启动脚本
To generate a Windows compatible startup script have a look to the excellent pm2-installer要生成与Windows兼容的启动脚本,请查看优秀的pm2-installer
Init systems supported支持的初始化系统
- systemd: Ubuntu >= 16, CentOS >= 7, Arch, Debian >= 7
- upstart: Ubuntu <= 14
- launchd: Darwin, MacOSx
- openrc: Gentoo Linux, Arch Linux
- rcd: FreeBSD
- systemv: Centos 6, Amazon Linux
These init systems are automatically detected by PM2 with the PM2通过pm2 startup
command.pm2 startup
命令自动检测这些初始系统。