【Linux】PM2设置hexo开机自启动


hexo设置开机启动,可以执行pm2 start hexo-start.sh,利用pPM2后台管理hexo的启动.

那么新问题来了,每次我们需要重启服务器,都需要重新执行一下pm2 start hexo-start.sh命令,是不是很麻烦,有没有办法让PM2在开机时自动启动我们的应用? 以下提供两种方式: - startup 步骤如下:

[root@thexqf _posts]# vim hexo-start.sh
#!/bin/bash
cd /root/blog && hexo generate --watch

之后设置PM2

[root@thexqf _posts]# pm2 start hexo-start.sh
[PM2] Applying action restartProcessId on app [1](ids: [ '1' ])
[PM2] [hexo-start](1)[PM2] Process successfully started
┌─────┬───────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name          │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 1   │ hexo-start    │ default     │ 0.0.0   │ fork    │ 1459     │ 0s     │ 0    │ online    │ 0%       │ 2.6mb    │ root     │ disabled │
│ 0   │ hexo_run      │ default     │ 0.0.0   │ fork    │ 0088   │ stopped   │ 0%       │ 0b       │ root     │ disabled │
└─────┴───────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

然后再执行

[root@thexqf logs]# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2

再设置开机自启动

[root@thexqf .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/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/node/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/node/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/node/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/node/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
  • 使用rc.local 使用rc.local可能是相当过时的办法,但是对我而言,简单且好用就可以。首先创建rc.local文件,并设置执行权限
chmod u+x /etc/rc.local
vim /etc/rc.local

在rc.local文件第一行加入如下语句

#!/bin/sh -e

之后在rc.local中添加如下内容:

su -c '/usr/local/node/bin/pm2 start /root/blog/hexo-start.sh' --login root

其中-c, --login代表:

-c 指定command
--login Provide an environment similar to what the user would expect had the user logged in directly(指定某个用户的环境变量)

这样就会加载用户的环境,否则会出现类似如下错误:

/usr/bin/env: ‘node’: No such file or directory

因为在pm2的脚本中使用了如下内容:

#!/usr/bin/env node

最后设置rc.local开机自启动

systemctl enable rc-local

验证: 进行上述设置后,直接重启服务器,发现我们的服务访问正常,说明上述设置 无误,大功告成哈哈。

相关 调试及排错:

systemctl status rc-local # 查看rc-local的状态
/root/.pm2/pm2.log
/root/.pm2/logs/hexo-start-err.log
/root/.pm2/logs/hexo-start-outs.log # 这三个文件为PM2的日志及脚本运行过程中产生的日志