http://www.cnblogs.com/laien/p/5826080.html

一、简介

pm2是一个带有负载均衡功能的应用进程管理器,类似有Supervisor,forever。

二、安装

Linux Binaries下载地址:https://nodejs.org/dist

  1. cd oneinstack/src
  2. wget https://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-x64.tar.gz
  3. tar xzf node-v4.2.4-linux-x64.tar.gz
  4. cp node-v4.2.4-linux-x64/bin/node /usr/local/bin/
  5. cp -R node-v4.2.4-linux-x64/lib/node_modules /usr/local/lib/
  6. ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm
  7. npm install pm2@latest -g #安装最新版本pm2模块

PS: 如果你的主机无法连接公网,先找到能连公网的主机安装上面的方法安装pm2,然后拷贝到你要安装的主机。拷贝如下目录:

  1. /usr/local/bin/node
  2. /usr/local/lib/node_modules

再创建相关软连接

三、PM2常用命令

假设你现在已经写好了一个app.js的文件,需要启动,你可以使用pm2进行管理

1. 启动

  1. # pm2 start app.js
  2. # pm2 start app.js --name my-api #my-api为PM2进程名称
  3. # pm2 start app.js -i 0 #根据CPU核数启动进程个数
  4. # pm2 start app.js --watch #实时监控app.js的方式启动,当app.js文件有变动时,pm2会自动reload

2. 查看进程

  1. # pm2 list
  2. # pm2 show 0 或者 # pm2 info 0 #查看进程详细信息,0为PM2进程id

3. 监控

  1. # pm2 monit

4. 停止

  1. # pm2 stop all #停止PM2列表中所有的进程
  2. # pm2 stop 0 #停止PM2列表中进程为0的进程

5. 重载

  1. # pm2 reload all #重载PM2列表中所有的进程
  2. # pm2 reload 0 #重载PM2列表中进程为0的进程

6. 重启

  1. # pm2 restart all #重启PM2列表中所有的进程
  2. # pm2 restart 0 #重启PM2列表中进程为0的进程

7. 删除PM2进程

  1. # pm2 delete 0 #删除PM2列表中进程为0的进程
  2. # pm2 delete all #删除PM2列表中所有的进程

8. 日志操作

  1. # pm2 logs [--raw] #Display all processes logs in streaming
  2. # pm2 flush #Empty all log file
  3. # pm2 reloadLogs #Reload all logs

9. 升级PM2

  1. # npm install pm2@lastest -g #安装最新的PM2版本
  2. # pm2 updatePM2 #升级pm2

10. 更多命令参数请查看帮助

  1. # pm2 --help

四、PM2目录结构

默认的目录是:当前用于的家目录下的.pm2目录(此目录可以自定义,请参考:五、自定义启动文件),详细信息如下:

  1. $HOME/.pm2 #will contain all PM2 related files
  2. $HOME/.pm2/logs #will contain all applications logs
  3. $HOME/.pm2/pids #will contain all applications pids
  4. $HOME/.pm2/pm2.log #PM2 logs
  5. $HOME/.pm2/pm2.pid #PM2 pid
  6. $HOME/.pm2/rpc.sock #Socket file for remote commands
  7. $HOME/.pm2/pub.sock #Socket file for publishable events
  8. $HOME/.pm2/conf.js #PM2 Configuration

五、自定义启动文件

创建一个test.json的示例文件,格式如下:

  1. {
  2. "apps":
  3. {
  4. "name": "test",
  5. "cwd": "/data/wwwroot/nodejs",
  6. "script": "./test.sh",
  7. "exec_interpreter": "bash",
  8. "min_uptime": "60s",
  9. "max_restarts": 30,
  10. "exec_mode" : "cluster_mode",
  11. "error_file" : "./test-err.log",
  12. "out_file": "./test-out.log",
  13. "pid_file": "./test.pid"
  14. "watch": false
  15. }
  16. }

说明:

apps:json结构,apps是一个数组,每一个数组成员就是对应一个pm2中运行的应用

name:应用程序的名称

cwd:应用程序所在的目录

script:应用程序的脚本路径

exec_interpreter:应用程序的脚本类型,这里使用的shell,默认是nodejs

min_uptime:最小运行时间,这里设置的是60s即如果应用程序在60s内退出,pm2会认为程序异常退出,此时触发重启max_restarts设置数量

max_restarts:设置应用程序异常退出重启的次数,默认15次(从0开始计数)

exec_mode:应用程序启动模式,这里设置的是cluster_mode(集群),默认是fork

error_file:自定义应用程序的错误日志文件

out_file:自定义应用程序日志文件

pid_file:自定义应用程序的pid文件

watch:是否启用监控模式,默认是false。如果设置成true,当应用程序变动时,pm2会自动重载。这里也可以设置你要监控的文件。

详细参数列表:见附件八

六、实例

已上面的test.json为例

  1. # cat > /data/wwwroot/nodejs/test.sh << EOF
  2. #!/bin/bash
  3. while :
  4. do
  5. echo "Test" >> 1.log
  6. sleep 5
  7. done
  8. EOF
  1. # chmod +x test.sh #添加执行权限
  2. # pm2 start test.json #启动,如下图:
  1. # pm2 list #查看pm2进程,如下图:

七、备注

其他可参数见官网:http://pm2.keymetrics.io

八、附件

Field Type Example Description
name string "myAPI" name your app will have in PM2
script string "bin/app.js" path of your app
args list ["--enable-logs", "-n", "15"] arguments given to your app when it is launched
node_args list ["--harmony", "--max-stack-size=1024"] arguments given to node when it is launched
cwd string "/var/www/app/prod" the directory from which your app will be launched
exec_mode string "cluster" "fork" mode is used by default, "cluster" mode can be configured with instances field
instances number 4 number of instances for your clustered app, 0 means as much instances as you have CPU cores. a negative value means CPU cores - value (e.g -1 on a 4 cores machine will spawn 3 instances)
exec_interpreter string "node" defaults to "node". can be "python", "ruby", "bash" or whatever interpreter you wish to use. "none" will execute your app as a binary executable
log_date_format string "YYYY-MM-DD HH:mm Z" format in which timestamps will be displayed in the logs
error_file string "/var/log/node-app/node-app.stderr.log" path to the specified error log file. PM2 generates one by default if not specified and you can find it by typing pm2 desc <app id>
out_file string "/var/log/node-app/node-app.stdout.log" path to the specified output log file. PM2 generates one by default if not specified and you can find it by typing pm2 desc <app id>
pid_file string "pids/node-geo-api.pid" path to the specified pid file. PM2 generates one by default if not specified and you can find it by typing pm2 desc <app id>
merge_logs boolean false defaults to false. if true, it will merge logs from all instances of the same app into the same file
cron_restart string "1 0 * * *" a cron pattern to restart your app. only works in "cluster" mode for now. soon to be avaible in "fork" mode as well
watch boolean true enables the watch feature, defaults to "false". if true, it will restart your app everytime a file change is detected on the folder or subfolder of your app.
ignore_watch list ["[\/\\]\./", "node_modules"] list of regex to ignore some file or folder names by the watch feature
min_uptime number 1000 min uptime of the app to be considered started (i.e. if the app crashes in this time frame, the app will only be restarted the number set in max_restarts (default 15), after that it's errored)
max_restarts number 10 number of consecutive unstable restarts (less than 1sec interval or custom time via min_uptime) before your app is considered errored and stop being
max_memory_restart string "150M" your app will be restarted by PM2 if it exceeds the amount of memory specified. human-friendly format : it can be "10M", "100K", "2G" and so on...
env object {"NODE_ENV": "production", "ID": "42"} env variables which will appear in your app
autorestart boolean false true by default. if false, PM2 will not restart your app if it crashes or ends peacefully
vizion boolean false true by default. if false, PM2 will start without vizion features (versioning control metadatas)
post_update list ["npm install", "echo launching the app"] a list of commands which will be executed after you perform a Pull/Upgrade operation from Keymetrics dashboard
force boolean true defaults to false. if true, you can start the same script several times which is usually not allowed by PM2
next_gen_js boolean true defaults to false. if true, PM2 will launch your app using embedded BabelJS features which means you can run ES6/ES7 javascript code
restart_delay number 4000 time to wait before restarting a crashed app (in milliseconds). defaults to 0.

转载于:https://www.cnblogs.com/zaifeng0108/p/7226488.html

nodejs pm2教程(转载)相关推荐

  1. nodejs pm2使用

    pm2特点 1.简单方便,部署快. 2.解决nodejs单线程缺点 3.完善的日志 pm2安装及使用 1.安装 npm install -g pm2 2.启动 查看版本 pm2 start app.j ...

  2. 文档笔记----nodejs菜鸟教程

    <html><head><meta charset="utf-8"><title>文档笔记----nodejs菜鸟教程</ti ...

  3. hibernate官方新手教程 (转载)

    hibernate官方新手教程第一部分 - 第一个Hibernate程序 首先我们将创建一个简单的控制台(console-based)Hibernate程序.我们使用内置数据库(in-memory d ...

  4. 【教程转载】 火狐浏览器 实用插件一览(附插件下载地址)

    [教程转载] 火狐浏览器 实用插件一览(附插件下载地址)简单介绍一下: 火狐浏览器(firefox)最大的优点就是安全.快速.稳定. 有些人抱怨火狐的功能太少,有些网页不能正常浏览. 没关系,这些都不 ...

  5. NODEJS - EJS教程

    NodeJs - EJS 教程 NodeJs - EJS 教程 EJS介绍 安装EJS 例子 补充说明 EJS介绍 EJS是一种简单的模板语言,可以使用原始的JavaScript生成HTML,类似JA ...

  6. 无师自通-自己学写脚本,小明外挂脚本代码教程 转载

    无师自通-自己学写脚本,小明外挂脚本代码教程 转载 2011-02-03 19:47:20| 分类: 石器脚本 assa | 标签:walkpos 指令 对话框 跳转 道具 |字号 订阅 下载LOFT ...

  7. 万字肝完nodejs入门教程,详解入口,建议收藏(更新中)

    nodejs入门教程 1.前言 2.入门教程 2.1 命令行运行node.js脚本 2.2 退出node.js程序 2.3 如何使用 Node.js REPL 2.3.1 使用方式 2.3.2 点命令 ...

  8. Arcgis安装教程——转载

    Arcgis安装教程--转载 转自https://blog.csdn.net/qq_36213352/article/details/80646940

  9. nodejs pm2的简单应用

    2019独角兽企业重金招聘Python工程师标准>>> 一.简介 pm2是一个带有负载均衡功能的应用进程管理器,类似有Supervisor,forever,详细参数见官网:http: ...

最新文章

  1. Qt 常量中有换行符 中文
  2. UIbutton系统按键(System)和图片按键(Custom)对比
  3. 高等数学上-赵立军-北京大学出版社-题解-练习4.5
  4. PHP统计API调用,php – 记录API调用
  5. 两个文件比较之comm命令
  6. 数据安全架构设计与实战~思维导图
  7. 已解决——pycharm在同目录下import,pycharm会提示错误,但是可以运行
  8. Robocode教程3——Robo机器剖析
  9. ExtraPhrase:一种针对抽象式(生成式)摘要的数据增强方法
  10. 树状数组专题【完结】
  11. 谈谈优化JDBC数据库编程(转)
  12. 技术架构图-大数据架构
  13. R 计算变量之间的相关性
  14. 世界最大最高摩天轮,迪拜之眼迎来首批游客
  15. 13_Pinia组件库
  16. I.MX6Q(TQIMX6Q/TQE9)学习笔记——新版BSP之声卡驱动移植
  17. Elasticsearch深度探秘搜索技术基于multi_match语法实现dis_max+tie_breaker
  18. 【自动驾驶】1.V2X、OBU、RSU、V2V之间的协作关系
  19. 【dfs序+树状数组】多次更新+求结点子树和操作,牛客小白月赛24 I题 求和
  20. 【机器学习】通过ID3,C4.5,CART算法构建决策树

热门文章

  1. 蓝桥杯集训之开训第一课
  2. 绿米开关如何重置_智能家居基础配置之人体传感器—绿米Aqara 人体传感器
  3. 学习C++项目—— 搭建多线程网络服务框架,性能测试(并发性能测试,业务性能测试,客户端响应时间测试,网络带宽测试)
  4. 用幂次变换来增强图像matlab,基于幂次变换及MSR光照不均图像增强.doc
  5. oracle估算数据增长,如何估算oracle 数据库,数据库对象历史增长情况
  6. Ext2 中如何换行 布局?
  7. es6-let const
  8. 基于Nginx的Wesocket负载均衡
  9. 《Adobe After Effects CS5经典教程》——1.5 对合成图像作动画处理
  10. 用SDWebImage渐变加载图片