使用 pm2 管理 node等 服务

安装pm2

官网

PM2 is a process manager. It manages your applications states, so you can start, stop, restart and delete processes.

pm2需要全局安装

cnpm install pm2@latest -g

使用

进入项目根目录

Start an app

pm2 start app.js

Or start any other application easily:

pm2 start bashscript.sh
pm2 start python-app.py --watch
pm2 start binary-file -- --port 1520

As you can see many options are available to manage your application with PM2. You will discover them depending on your use case.

  • cli 参数
  • --name <app_name>:Specify an app name
  • --watch:Watch and Restart app when files change
  • --max-memory-restart <200MB>:Set memory threshold for app reload
  • --log <log_path>:Specify log file
  • -- arg1 arg2 arg3:Pass extra arguments to the script
  • --restart-delay <delay in ms>:Delay between automatic restarts
  • --time:Prefix logs with time
  • --no-autorestart:Do not auto restart app
  • --cron <cron_pattern>:Specify cron for forced restart
  • --no-daemon:Attach to application log

Managing processes

Managing application state is simple here are the commands

pm2 restart app_name
pm2 reload app_name
pm2 stop app_name
pm2 delete app_name

Instead of app_name you can pass:

  • all to act on all processes
  • id to act on a specific process id

Check status, logs, metrics

List managed applications

List the status of all application managed by PM2

pm2 [list|ls|status]

Display logs

To display logs in realtime:

pm2 logs

To dig in older logs:

pm2 logs --lines 200

Terminal Based Dashboard

Here is a realtime dashboard that fits directly into your terminal:

pm2 monit

pm2.io: Monitoring & Diagnostic Web Interface

pm2 plus

Updating PM2

pm2 update

CheatSheet

Here are some commands that are worth knowing. Just try them with a sample application or with your current web application on your development machine:

# Fork mode
pm2 start app.js --name my-api # Name process

# Cluster mode
pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
pm2 start app.js -i max      # Same as above, but deprecated.
pm2 scale app +3             # Scales `app` up by 3 workers
pm2 scale app 2              # Scales `app` up or down to 2 workers total

# Listing

pm2 list               # Display all processes status
pm2 jlist              # Print process list in raw JSON
pm2 prettylist         # Print process list in beautified JSON

pm2 describe 0         # Display all informations about a specific process

pm2 monit              # Monitor all processes

# Logs

pm2 logs [--raw]       # Display all processes logs in streaming
pm2 flush              # Empty all log files
pm2 reloadLogs         # Reload all logs

# Actions

pm2 stop all           # Stop all processes
pm2 restart all        # Restart all processes

pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)

pm2 stop 0             # Stop specific process id
pm2 restart 0          # Restart specific process id

pm2 delete 0           # Will remove process from pm2 list
pm2 delete all         # Will remove all processes from pm2 list

# Misc

pm2 reset <process>    # Reset meta data (restarted time...)
pm2 updatePM2          # Update in memory pm2
pm2 ping               # Ensure pm2 daemon has been launched
pm2 sendSignal SIGUSR2 my-app # Send system signal to script
pm2 start app.js --no-daemon
pm2 start app.js --no-vizion
pm2 start app.js --no-autorestart
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值