这进程管理工具很nice——pm2

官网:https://pm2.keymetrics.io/

介绍

PM2是一个流行的Node.js进程管理器。它允许你简化Node.js应用程序的部署和管理。PM2可以帮助你在生产环境中管理Node.js进程,包括启动、停止、重启应用程序,以及监视应用程序的状态和日志。它还具有一些其他功能,比如负载均衡、自动重新启动、内存监控等。 PM2还提供了一个简单的命令行界面,使得管理和监控Node.js应用程序变得更加方便。

常用命令

启动应用程序

pm2 start <app.js>

停止应用程序

pm2 stop <app_name|app_id>

重启应用程序

pm2 restart <app_name|app_id>

重新加载应用程序(不关闭服务):

pm2 reload <app_name|app_id>

查看应用程序列表

pm2 list

查看应用程序详细信息

pm2 show <app_name|app_id>

查看实时日志

pm2 logs <app_name|app_id>

清除日志

pm2 flush

监视模式启动应用程序(会自动重启):

pm2 start <app.js> --watch

设置应用程序的数量(用于负载均衡):

pm2 scale <app_name|app_id> <number_of_instances>

这只是其中的一部分命令,PM2还有更多功能和选项,你可以通过查阅官方文档来获取更多信息。

案例

使用pm2管理hexo 进程,使其可以在后台持续运行

  1. 安装

    npm install -g pm2
    
  2. 在hexo根目录新建运行文件,如hexo_run.js

    //run
    const { exec } = require('child_process')
    exec('hexo server',(error, stdout, stderr) => {
            if(error){
                    console.log('exec error: ${error}')
                    return
            }
            console.log('stdout: ${stdout}');
            console.log('stderr: ${stderr}');
    })
    
  3. 启动进程服务

    [root@Lys-Server hexo-blog]# pm2 start hexo_run.js 
    [PM2] Starting /root/hexo/hexo-blog-originfile/hexo_run.js in fork_mode (1 instance)
    [PM2] Done.
    ┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
    │ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
    ├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
    │ 0  │ hexo_run           │ fork     │ 0    │ online    │ 0%       │ 15.3mb   │
    └────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
    
  4. 这样就OK了😁

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员-小李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值