Welcome to the PM2 Quick Start!
PM2 is a daemon process manager that will help you manage and keep your application online. Getting started with PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via NPM.
Installation
The latest PM2 version is installable with NPM or Yarn:
1 | $ npm install pm2@latest -g |
To install Node.js and NPM you can use NVM
Start an app
The simplest way to start, daemonize and monitor your application is by using this command line:
1 | $ pm2 start app.js |
Or start any other application easily:
1 | $ pm2 start bashscript.sh |
Some options you can pass to the CLI:
1 | # Specify an app name |
As you can see many options are available to manage your application with PM2. You will discover them depending on your use case.
Managing processes
Managing application state is simple here are the commands:
1 | $ pm2 restart app_name |
Instead of app_name
you can pass:
all
to act on all processesid
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 managed applications
List the status of all application managed by PM2:
1 | $ pm2 [list|ls|status] |
Display logs
To display logs in realtime:
1 | $ pm2 logs |
To dig in older logs:
1 | $ pm2 logs --lines 200 |
Terminal Based Dashboard
Here is a realtime dashboard that fits directly into your terminal:
1 | $ pm2 monit |
pm2.io: Monitoring & Diagnostic Web Interface
Web based dashboard, cross servers with diagnostic system:
1 | $ 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:
1 | $ 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:
1 | $ pm2 ecosystem |
This will generate and ecosystem.config.js file:
1 | module.exports = { |
And start it easily:
1 | $ pm2 start process.yml |
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:
1 | $ pm2 startup |
And to freeze a process list for automatic respawn:
1 | $ pm2 save |
Read more about startup script generator here.
Restart application on changes
It’s pretty easy with the --watch
option:
1 | $ cd /path/to/my/app |
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.
Updating PM2
We made it simple, there is no breaking change between releases and the procedure is straightforward:
1 | npm install pm2@latest -g |
Then update the in-memory PM2 :
1 | 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:
1 | # Fork mode |
What’s next?
Learn how to declare all your application’s behavior options into a JSON configuration file.
Learn how to do clean stop and restart to increase reliability.
Learn how to deploy and update production applications easily.
Monitor your production applications with Keymetrics.
How to update PM2
Install the latest pm2 version:
1 | npm install pm2@latest -g |
Then update the in-memory PM2 :
1 | pm2 update |