hexo进入后台运行
作者:互联网
配置后台运行,安装pm2
npm install -g pm2
ln -s /opt/node-v14.18.3-linux-x64/bin/pm2 /usr/local/bin/pm2
在博客根目录下创建一个文件 run.js
vim run.js
const { exec } = require('child_process')
exec('hexo server -p 8000',(error, stdout, stderr) => {
if(error){
console.log('exec error: ${error}')
return
}
console.log('stdout: ${stdout}');
console.log('stderr: ${stderr}');
})
使用pm2运行脚本 run.js
pm2 start run.js
# 运行起来后可以使用pm2 show + 进程pid查看后台hexo进行
pm2 start run.js
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /webapp/run.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ run │ default │ 0.0.0 │ fork │ 5316 │ 0s │ 0 │ online │ 0% │ 18.4mb │ root │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
# 查看状态:
pm2 show 0
Describing process with id 0 - name run
┌───────────────────┬───────────────────────────────┐
│ status │ online │
│ name │ run │
│ namespace │ default │
│ version │ 0.0.0 │
│ restarts │ 0 │
│ uptime │ 24s │
│ script path │ /webapp/run.js │
│ script args │ N/A │
│ error log path │ /root/.pm2/logs/run-error.log │
│ out log path │ /root/.pm2/logs/run-out.log │
│ pid path │ /root/.pm2/pids/run-0.pid │
│ interpreter │ node │
│ interpreter args │ N/A │
│ script id │ 0 │
│ exec cwd │ /webapp │
│ exec mode │ fork_mode │
│ node.js version │ xx.xx.xx │
│ node env │ N/A │
│ watch & reload │ ✘ │
│ unstable restarts │ 0 │
│ created at │ 2022-04-29T09:43:05.581Z │
└───────────────────┴───────────────────────────────┘
Actions available
┌────────────────────────┐
│ km:heapdump │
│ km:cpu:profiling:start │
│ km:cpu:profiling:stop │
│ km:heap:sampling:start │
│ km:heap:sampling:stop │
└────────────────────────┘
Trigger via: pm2 trigger run <action_name>
Code metrics value
┌────────────────────────┬──────────┐
│ Used Heap Size │ 4.55 MiB │
│ Heap Usage │ 78.05 % │
│ Heap Size │ 5.82 MiB │
│ Event Loop Latency p95 │ 1.83 ms │
│ Event Loop Latency │ 0.31 ms │
│ Active handles │ 6 │
│ Active requests │ 0 │
└────────────────────────┴──────────┘
Divergent env variables from local env
Add your own code metrics: http://bit.ly/code-metrics
Use `pm2 logs run [--lines 1000]` to display logs
Use `pm2 env 0` to display environment variables
Use `pm2 monit` to monitor CPU and Memory usage run
标签:run,hexo,pm2,js,PM2,error,后台,运行,log 来源: https://www.cnblogs.com/xjzyy/p/16207568.html