zoukankan      html  css  js  c++  java
  • Node.js pm2的使用

    pm2 是一个带有负载均衡功能的Node应用的进程管理器。可以把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载。

    pm2的主要特性:

    1、内建负载均衡(使用Node cluster 集群模块) 
    2、后台运行 
    3、0秒停机重载 
    4、具有Ubuntu和CentOS 的启动脚本 
    5、停止不稳定的进程(避免无限循环) 
    6、控制台检测 
    7、提供 HTTP API 
    8、远程控制和实时的接口API ( Nodejs 模块,允许和PM2进程管理器交互 )

    pm2的安装:

    npm install -g pm2

    pm2的用法:

    pm2 start app.js -i 4   // 后台运行pm2,启动4个app.js 
                            // 也可以把'max' 参数传递给 start
                            // 正确的进程数目依赖于Cpu的核心数目
    pm2 start app.js --name my-api // 命名进程
    pm2 list               // 显示所有进程状态
    pm2 monit              // 监视所有进程
    pm2 logs               //  显示所有进程日志
    pm2 stop all           // 停止所有进程
    pm2 restart all        // 重启所有进程
    pm2 reload all         // 0秒停机重载进程 (用于 NETWORKED 进程)
    pm2 stop 0             // 停止指定的进程
    pm2 restart 0          // 重启指定的进程
    pm2 startup            // 产生 init 脚本 保持进程活着
    pm2 web                // 运行健壮的 computer API endpoint 
    pm2 delete 0           // 杀死指定的进程
    pm2 delete all         // 杀死全部进程

    pm2运行进程的不同方式:
    pm2 start app.js -i max  // 根据有效CPU数目启动最大进程数目
    pm2 start app.js -i 3      // 启动3个进程
    pm2 start app.js -x        //用fork模式启动 app.js 而不是使用 cluster
    pm2 start app.js -x -- -a 23   // 用fork模式启动 app.js 并且传递参数 (-a 23)
    pm2 start app.js --name serverone  // 启动一个进程并把它命名为 serverone
    pm2 stop serverone       // 停止 serverone 进程
    pm2 start app.json        // 启动进程, 在 app.json里设置选项
    pm2 start app.js -i max -- -a 23                   //在--之后给 app.js 传递参数
    pm2 start app.js -i max -e err.log -o out.log  // 启动并生成一个配置文件
    
    // 也可以执行用其他语言编写的app  ( fork 模式):
    pm2 start my-bash-script.sh    -x --interpreter bash
    pm2 start my-python-script.py -x --interpreter python

    常用命令使用:

    pm2 list

    列出由pm2管理的所有进程信息,还会显示一个进程会被启动多少次,因为没处理的异常。

    pm2 monit

    监视每个node进程的CPU和内存的使用情况。

  • 相关阅读:
    MySql 范式
    MySql 多表关系
    MySql 约束条件
    MySql 枚举和集合 详解
    【RoR win32】新建rails项目找不到script/server的解决办法
    【RoR win32】安装RoR
    【RoR win32】提高rails new时bundle install运行速度
    【bs4】安装beautifulsoup
    【py分析网页】可能有用的-re去除网页上的杂碎
    【pyQuery】抓取startup news首页
  • 原文地址:https://www.cnblogs.com/void9main/p/9407879.html
Copyright © 2011-2022 走看看