1、首先在项目根目录使用 pm2 启动项目
pm2 start ./dist/main.js
2、生成开机启动 pm2 服务的配置文件
运行:pm2 startup 命令,会生成以下命令
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/git/bin:/usr/local/n/versions/node/12.22.9/bin:/root/android-sdk/platform-tools:/root/android-sdk/tools:/opt/gradle-5.5/bin:/usr/local/java/jdk8u272-b10/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python3/bin:/root/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure
ExecStart=/usr/local/n/versions/node/12.22.9/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/n/versions/node/12.22.9/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/n/versions/node/12.22.9/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
[root@localhost ktt-spider]# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2
3、执行 pm2 save
保存当前 pm2 运行的各个应用保存到 /home/user1/.pm2/dump.pm2 下,开机重启时会读取该文件中的内容启动相关应用
重启试试!
标签:bin,PM2,pm2,local,开机,usr,自启动,root From: https://www.cnblogs.com/Denny_Yang/p/17325835.html