logo
🚀 DevOps

PM2

PM2 Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · DevOps🧭 Markdown 速查🏷️ 2 个标签
#pm2#process
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Install

The latest PM2 version is installable with NPM or Yarn

SHELL
滚动查看更多
$ npm install pm2@latest -g

OR

SHELL
滚动查看更多
$ yarn global add pm2
Start an app

The simplest way to start, daemonize and monitor your application is by using this command line

SHELL
滚动查看更多
$ pm2 start app.js

Or start any other application easily

SHELL
滚动查看更多
$ pm2 start bashscript.sh
$ pm2 start python-app.py --watch
$ pm2 start binary-file -- --port 1520
Some options you can pass to the CLI

Specify an app name

SHELL
滚动查看更多
--name <app_name>

Watch and Restart app when files change

SHELL
滚动查看更多
--watch

Set memory threshold for app reload

SHELL
滚动查看更多
--max-memory-restart <200MB>

Specify log file

SHELL
滚动查看更多
--log <log_path>

Pass extra arguments to the script

SHELL
滚动查看更多
-- arg1 arg2 arg3

Delay between automatic restarts

SHELL
滚动查看更多
--restart-delay <delay in ms>

Prefix logs with time

SHELL
滚动查看更多
--time

Do not auto restart app

SHELL
滚动查看更多
--no-autorestart

Specify cron for forced restart

SHELL
滚动查看更多
--cron <cron_pattern>

Attach to application log

SHELL
滚动查看更多
--no-daemon
Managing processes

Managing application state is simple here are the commands

SHELL
滚动查看更多
$ 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

Now that you have started this application, you can check its status, logs, metrics and even get the online dashboard with pm2.io.

List the status of all application managed by PM2

SHELL
滚动查看更多
$ pm2 [list|ls|status]

To display logs in realtime

SHELL
滚动查看更多
$ pm2 logs

To dig in older logs

SHELL
滚动查看更多
$ pm2 logs --lines 200

Here is a realtime dashboard that fits directly into your terminal

SHELL
滚动查看更多
$ pm2 monit

Web based dashboard, cross servers with diagnostic system

SHELL
滚动查看更多
$ pm2 plus
Cluster mode

For Node.js applications, PM2 includes an automatic load balancer that will share all HTTP[s]/Websocket/TCP/UDP connections between each spawned processes.

To start an application in Cluster mode

SHELL
滚动查看更多
$ pm2 start app.js -i max

Read more about cluster mode here.

Ecosystem File

You can also create a configuration file, called Ecosystem File, to manage multiple applications. To generate an Ecosystem file

SHELL
滚动查看更多
$ pm2 ecosystem

This will generate an ecosystem.config.js file

CODE
滚动查看更多
module.exports = {
  apps : [{
    name: "app",
    script: "./app.js",
    env: {
      NODE_ENV: "development",
    },
    env_production: {
      NODE_ENV: "production",
    }
  }, {
     name: 'worker',
     script: 'worker.js'
  }]
}

And start it easily

SHELL
滚动查看更多
$ pm2 start ecosystem.config.js

Read more about application declaration here

Setup startup script

Restarting PM2 with the processes you manage on server boot/reboot is critical. To solve this, just run this command to generate an active startup script

SHELL
滚动查看更多
$ pm2 save

Read more about startup script generator here

Restart application on changes

It’s pretty easy with the --watch option

CODE
滚动查看更多
$ cd /path/to/my/app
$ pm2 start env.js --watch --ignore-watch="node_modules"

This will watch & restart the app on any file change from the current directory + all subfolders and it will ignore any changes in the node_modules folder --ignore-watch="node_modules".

You can then use pm2 logs to check for restarted app logs.


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

SHELL
滚动查看更多
$ pm2 start app.js --name my-api # Name process

Cluster mode

SHELL
滚动查看更多
$ 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

SHELL
滚动查看更多
$ 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 information about a specific process

$ pm2 monit              # Monitor all processes

Logs

SHELL
滚动查看更多
$ pm2 logs [--raw]       # Display all processes logs in streaming
$ pm2 flush              # Empty all log files
$ pm2 reloadLogs         # Reload all logs

Actions

SHELL
滚动查看更多
$ 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

SHELL
滚动查看更多
$ 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
Updating PM2

We made it simple, there is no breaking change between releases and the procedure is straightforward

SHELL
滚动查看更多
$ npm install pm2@latest -g

Then update the in-memory PM2

SHELL
滚动查看更多
$ pm2 update

Also see

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572