一、官方文档:
官方相关文档:http://pm2.keymetrics.io/docs/usage/startup/#generating-a-startup-script
二、具体操作过程如下:
1、生成开机启动 pm2 服务的配置文件
运行:pm2 startup 命令
➜ ~ pm2 startup [PM2] Init System found: systemd [PM2] To setup the Startup Script, copy/paste the following command: sudo env PATH=$PATH:/home/user1/.nvm/versions/node/v8.8.1/bin /home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2
startup systemd -u user1 --hp /home/user1
粘贴上面的命令到终端,如下:
➜ init.d sudo env PATH=$PATH:/home/user1/.nvm/versions/node/v8.8.1/bin /home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2
startup systemd -u user1 --hp /home/user1 [sudo] user1 的密码:
在非root用户下会让你输入密码。密码输入完成后终端会出现如下内容:
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=user1 LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Environment=PATH=/home/user1/.nvm/versions/node/v8.8.1/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin Environment=PM2_HOME=/home/user1/.pm2 PIDFile=/home/user1/.pm2/pm2.pid ExecStart=/home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2 resurrect ExecReload=/home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2 reload all ExecStop=/home/user1/.nvm/versions/node/v8.8.1/lib/node_modules/pm2/bin/pm2 kill [Install] WantedBy=multi-user.target Target path /etc/systemd/system/pm2-user1.service Command list [ 'chmod +x /etc/systemd/system/pm2-user1.service', 'systemctl enable pm2-user1', 'systemctl start pm2-user1', 'systemctl daemon-reload', 'systemctl status pm2-user1' ] [PM2] Writing init configuration in /etc/systemd/system/pm2-user1.service [PM2] Making script booting at startup... >>> Executing chmod +x /etc/systemd/system/pm2-user1.service [DONE] >>> Executing systemctl enable pm2-user1 Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-user1.service to /etc/systemd/system/pm2-user1.service. [DONE] >>> Executing systemctl start pm2-user1 [DONE] >>> Executing systemctl daemon-reload [DONE] >>> Executing systemctl status pm2-user1 ● pm2-user1.service - PM2 process manager Loaded: loaded (/etc/systemd/system/pm2-user1.service; enabled; vendor preset: disabled) Active: active (running) since �� 2018-02-09 13:17:38 CST; 47ms ago Docs: https://pm2.keymetrics.io/ Main PID: 24295 (PM2 v2.7.2: God) CGroup: /system.slice/pm2-user1.service ‣ 24295 PM2 v2.7.2: God Daemon (/home/user1/.pm2) 2�� 09 13:17:38 izbp14un3b3r5tbimwuxlbz systemd[1]: Starting PM2 process manager... 2�� 09 13:17:38 izbp14un3b3r5tbimwuxlbz pm2[6857]: [PM2] Resurrecting 2�� 09 13:17:38 izbp14un3b3r5tbimwuxlbz systemd[1]: Started PM2 process manager. [DONE] +---------------------------------------+ [PM2] Freeze a process list on reboot via: $ pm2 save [PM2] Remove init script via: $ pm2 unstartup systemd
按照提示的命令进行配置就好了。你会在/etc/systemd/system文件夹下看到pm2-user1.service文件
2、pm2 save
, 保存当前 pm2 运行的各个应用保存到 /home/user1/.pm2/dump.pm2下,开机重启时读取该文件中的内容启动相关应用。
➜ ~ pm2 save [PM2] Saving current process list... [PM2] Successfully saved in /home/user1/.pm2/dump.pm2
3、查看配置是否开启:使用命令:systemctl status pm2-user1.service
➜ system systemctl status pm2-user1.service ● pm2-user1.service - PM2 process manager Loaded: loaded (/etc/systemd/system/pm2-user1.service; enabled; vendor preset: disabled) Active: active (running) since �� 2018-02-09 13:17:38 CST; 44min ago Docs: https://pm2.keymetrics.io/ Main PID: 24295 (PM2 v2.7.2: God) CGroup: /system.slice/pm2-user1.service ‣ 24295 PM2 v2.7.2: God Daemon (/home/user1/.pm2) 2�� 09 13:17:38 izbp14un3b3r5tbimwuxlbz pm2[6857]: [PM2] Resurrecting
参考:http://imweb.io/topic/57c8cbb27f226f687b365636
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html