PM2 — Persistent Node.js Process Management

Notes on using PM2 to keep a Node.js application (Hexo) running persistently and auto-restarting after system reboot.

1. Install PM2

1
npm install pm2@latest -g

2. Create a startup config

1
2
3
4
5
6
7
8
9
10
11
12
module.exports = {
apps: [{
name: 'Hexo',
script: 'hexo',
args: 'server -p 4000',
watch: true,
log_file: 'server.log',
merge_logs: true,
out_file: 'NULL',
error_file: 'NULL'
}]
};

3. Start Hexo

1
pm2 start start.config.js

4. Configure autostart

1
2
pm2 startup
pm2 save

5. Common commands

1
2
3
4
5
pm2 list
pm2 restart Hexo
pm2 stop Hexo
pm2 delete Hexo
pm2 save

Notes

  • For local preview only, PM2 is unnecessary — hexo server is enough.
  • For a stable long-running Hexo server on a VPS, PM2 is more reliable than keeping a terminal session open.
  • For GitHub Pages deployments, PM2 is not needed at all — you only run hexo generate and deploy static files.

记录如何用 PM2 让 Node.js 应用(Hexo)持久运行,并在系统重启后自动恢复。

1. 安装 PM2

1
npm install pm2@latest -g

2. 创建启动配置

1
2
3
4
5
6
7
8
9
10
11
12
module.exports = {
apps: [{
name: 'Hexo',
script: 'hexo',
args: 'server -p 4000',
watch: true,
log_file: 'server.log',
merge_logs: true,
out_file: 'NULL',
error_file: 'NULL'
}]
};

3. 启动 Hexo

1
pm2 start start.config.js

4. 配置开机自启

1
2
pm2 startup
pm2 save

5. 常用管理命令

1
2
3
4
5
pm2 list
pm2 restart Hexo
pm2 stop Hexo
pm2 delete Hexo
pm2 save

说明

  • 如果只是本地临时预览,不需要 PM2,直接 hexo server 即可。
  • 如果在 VPS 上长期运行 Hexo 服务,PM2 比手工保持终端会话更可靠。
  • GitHub Pages 场景下完全不需要 PM2,只需生成静态文件并部署。

PM2 — Persistent Node.js Process Management
https://blog.xiofelix.com/2024/01/12/pm2-hexo/
Author
Felix
Posted on
January 12, 2024
Licensed under