PM2常用命令

一、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

六、附件

FieldTypeExampleDescription
namestring"myAPI"name your app will have in PM2
scriptstring"bin/app.js"path of your app
argslist["--enable-logs", "-n", "15"]arguments given to your app when it is launched
node_argslist["--harmony", "--max-stack-size=1024"]arguments given to node when it is launched
cwdstring"/var/www/app/prod"the directory from which your app will be launched
exec_modestring"cluster""fork" mode is used by default, "cluster" mode can be configured with instances field
instancesnumber4number 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_interpreterstring"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_formatstring"YYYY-MM-DD HH:mm Z"format in which timestamps will be displayed in the logs
error_filestring"/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_filestring"/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_filestring"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_logsbooleanfalsedefaults to false. if true, it will merge logs from all instances of the same app into the same file
cron_restartstring"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
watchbooleantrueenables 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_watchlist["[\/\\]\./", "node_modules"]list of regex to ignore some file or folder names by the watch feature
min_uptimenumber1000min 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_restartsnumber10number 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_restartstring"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...
envobject{"NODE_ENV": "production", "ID": "42"}env variables which will appear in your app
autorestartbooleanfalsetrue by default. if false, PM2 will not restart your app if it crashes or ends peacefully
vizionbooleanfalsetrue by default. if false, PM2 will start without vizion features (versioning control metadatas)
post_updatelist["npm install", "echo launching the app"]a list of commands which will be executed after you perform a Pull/Upgrade operation from Keymetrics dashboard
forcebooleantruedefaults to false. if true, you can start the same script several times which is usually not allowed by PM2
next_gen_jsbooleantruedefaults to false. if true, PM2 will launch your app using embedded BabelJS features which means you can run ES6/ES7 javascript code
restart_delaynumber4000time to wait before restarting a crashed app (in milliseconds). defaults to 0.
<think>好的,用户想了解如何使用PM2启动应用程序,特别是示例命令。首先,我需要回忆一下PM2的基本用法。根据引用中的资料,PM2是一个Node.js进程管理工具,可以后台运行应用,并且支持多种配置。 用户提到了启动命令,所以首先应该给出最基本的启动方式。比如,直接使用`pm2 start app.js`,这可能已经在引用中提到了。例如,引用[2]和[3]都提到启动应用程序的命令,其中引用[2]还提到使用`--name`参数来命名进程,这样方便管理。 接下来,用户可能需要更多定制化的启动选项。比如设置进程名称、使用集群模式、传递环境变量等。引用[3]提到了用fork模式启动并传递参数,所以需要说明`--`后面跟参数的方法。同时,集群模式在引用中没有直接提到,但PM2支持cluster模式,可以补充这一点。 另外,配置文件的使用也很重要,引用[4]提到了使用`ecosystem.config.js`文件来定义启动配置,这样可以保存复杂的设置,方便部署。需要说明如何生成配置文件并启动。 用户可能还关心如何查看已启动的进程,引用[1]中的`pm2 list`命令需要包含在内。此外,日志管理和重启策略也是PM2的常用功能,应该简要提及。 需要确保所有命令示例正确,并正确引用相关的参考资料。例如,使用`--name`来自定义进程名来自引用[2],传递参数参考引用[3],配置文件的使用来自引用[4]。 最后,生成相关问题需要考虑用户可能的后续需求,比如进程监控、多环境配置、日志管理、集群模式等,这些问题可以帮助用户进一步了解PM2的高级功能。</think>### 如何使用PM2启动应用程序 PM2是Node.js应用进程管理工具,支持后台运行、负载均衡和自动重启等功能。以下是常用启动命令及说明: #### 1. **基础启动** 通过指定入口文件启动应用程序(默认以`fork`模式运行): ```bash pm2 start app.js ``` 若需自定义进程名称,使用`--name`参数: ```bash pm2 start app.js --name myapp # 进程名为"myapp"[^2] ``` #### 2. **传递参数** 启动时传递命令行参数,需在命令末尾添加`--`后接参数: ```bash pm2 start app.js -- -a 23 -p 8080 # 向app.js传递参数`-a 23 -p 8080`[^3] ``` #### 3. **集群模式** 启动多个实例以利用多核CPU(`-i`指定实例数): ```bash pm2 start app.js -i 4 # 启动4个实例(cluster模式) ``` #### 4. **配置文件启动** 通过`ecosystem.config.js`定义复杂配置(如环境变量、日志路径): ```bash pm2 init simple # 生成配置文件模板 pm2 start ecosystem.config.js # 根据配置文件启动[^4] ``` 配置文件示例: ```javascript module.exports = { apps: [{ name: "prod-server", script: "app.js", env: { NODE_ENV: "production", PORT: 3000 } }] } ``` #### 5. **其他常用操作** - 查看进程列表:`pm2 list`[^1] - 监视日志:`pm2 logs myapp` - 重启应用:`pm2 restart myapp` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值