PM2 commands cheat sheet
Installing and updating PM2
-
npm install pm2@latest -gInstalls PM2 as a global package, so the pm2 command is available from any directory. The same command also upgrades PM2 to the latest version. When the npm global installation directory is a system directory, the installation fails for a regular user with an EACCES error.
-
pm2 updateUpdates the PM2 background process (daemon) that runs in memory to the installed version. Run it after every PM2 upgrade.
-
pm2 pingChecks that the PM2 background process is running.
Starting an application
-
pm2 start app.jsStarts the application as a background process that PM2 manages. When the application crashes, PM2 restarts it automatically.
-
pm2 start app.js --name apiSets a name for the process. You then use this name in the restart, logs, and stop commands.
-
pm2 start "npm run start" --name webRuns a script from package.json through PM2, in this example the start script.
-
pm2 start app.js -- --port 3000Passes arguments to the application itself. Everything after -- goes to the application and not to PM2.
-
pm2 start app.js --watch --ignore-watch="node_modules"Restarts the application whenever its files change, and excludes the node_modules directory from watching. Suitable mainly for a development environment. On a production server, you restart deliberately after you deploy a new version.
-
pm2 start app.js --max-memory-restart 300MRestarts the application when its memory usage passes the threshold you set. Useful as protection against memory leaks.
-
pm2 start app.js --timeAdds a timestamp to every line in the application logs.
-
pm2 start app.js --cron "0 3 * * *"Restarts the application on a schedule in cron syntax, in this example every day at 3:00 AM.
-
pm2 start app.js --no-autorestartStarts the application without restarting it automatically when it exits or crashes. Suitable for a script that runs once.
-
pm2 start app.js --attachStarts the application and shows its logs right away. Ctrl+C exits the view, and the application keeps running.
Cluster mode
-
pm2 start app.js -i maxStarts the application in cluster mode, with a separate process for each CPU core, and PM2 distributes the requests among them. The -i 0 option does the same.
-
pm2 start app.js -i -1Starts one process for each core minus one, so one core stays free for the other services on the server.
-
pm2 start app.js -i 4Starts a fixed number of processes, in this example four.
-
pm2 scale api 2Changes the number of processes of an application running in cluster mode to an exact number, in this example two.
-
pm2 scale api +3Adds processes to the application, in this example three more.
Process list and status
-
pm2 listShows all the processes PM2 manages: ID, name, status, restart count, memory, and CPU. The aliases are pm2 ls and pm2 status.
-
pm2 list --sort memory:descSorts the list, in this example by memory usage in descending order. You can also sort by name, id, cpu, status, and uptime. Sorting works only with pm2 list and pm2 ls.
-
pm2 show apiShows all the details of one process: the script path, the log files, uptime, and the restart count. The alias is pm2 describe, and you can give a name or an ID.
-
pm2 monitOpens a monitoring dashboard in the terminal, with the memory and CPU usage of each process and its logs in real time.
-
pm2 jlistShows the process list in JSON format, for use in scripts. pm2 prettylist shows it as readable JSON.
-
pm2 reset apiResets the data collected for the process, such as the restart counter. pm2 reset all resets all the processes.
Restarting, stopping, and deleting
-
pm2 restart apiStops the process and starts it again. During the restart, the application is briefly unavailable. Instead of a name, you can give an ID, several names, or all.
-
pm2 reload apiA restart without downtime for a web application in cluster mode: the processes are replaced one after another, so there's always a process that answers requests. When the replacement doesn't finish within the set time, PM2 falls back to a regular restart. For an application that isn't running in cluster mode, reload performs a regular restart.
-
pm2 reload allReloads all the processes. pm2 restart all restarts all of them.
-
NODE_ENV=production pm2 restart api --update-envRestarts the process and also loads the environment variables that changed. Without --update-env, the process keeps the environment variables set when it was started.
-
pm2 stop apiStops the process. The process stays in the PM2 list, and you start it again with pm2 start api. An application started with --watch keeps restarting on every file change even after a regular stop, so you stop it with pm2 stop api --watch.
-
pm2 stop allStops all the processes.
-
pm2 delete apiCaution: stops the process and removes it from the PM2 list. To bring it back, you need to start it again with all its settings, and after pm2 save it also won't start when the server reboots.
-
pm2 delete allCaution: stops all the processes and removes all of them from the PM2 list.
-
pm2 sendSignal SIGUSR2 apiSends a system signal to the process, in this example SIGUSR2, for instance to an application that defines its own action for this signal.
Logs
-
pm2 logsShows the logs of all the processes in real time. Ctrl+C exits the view.
-
pm2 logs apiShows only the logs of a specific process, by name or ID.
-
pm2 logs api --lines 200Shows the last 200 lines of the log instead of the default 15.
-
pm2 logs --errShows only the error output. The --out option shows only the regular output.
-
~/.pm2/logsThe directory where logs are stored by default: a file named after the application with the -out.log suffix for the regular output, and a file with the -error.log suffix for errors. You can change the paths in the out_file and error_file fields of the ecosystem file.
-
pm2 flushCaution: empties all the log files of all the processes. pm2 flush api empties only the logs of a specific process.
-
pm2 reloadLogsReloads all the log files.
Log rotation: pm2-logrotate
-
pm2 install pm2-logrotateInstalls the pm2-logrotate module, which rotates the log files by size and time and deletes old files, so the logs don't fill the disk. You install it with pm2 install, not with npm install.
-
pm2 set pm2-logrotate:max_size 50MThe size at which a log file is rotated to a new file. The default is 10M.
-
pm2 set pm2-logrotate:retain 14The number of old files kept for each log. The default is 30.
-
pm2 set pm2-logrotate:compress trueCompresses the old log files. The default is false.
-
pm2 set pm2-logrotate:rotateInterval "0 0 * * *"A fixed rotation time in cron syntax, in addition to rotation by size. The default is every day at midnight.
-
pm2 confShows the current settings of PM2 and its modules, including pm2-logrotate.
The ecosystem file
-
pm2 init simpleCreates a sample ecosystem.config.js file in the current directory. In this file, you define all the applications and their settings in one place, and you keep it in the code repository. The name of a file you write by hand must end with .config.js.
-
module.exports = { apps: [{ name: "api", script: "./app.js", instances: "max", exec_mode: "cluster", max_memory_restart: "300M", env: { NODE_ENV: "development" }, env_production: { NODE_ENV: "production", PORT: 3000 } }] }A sample ecosystem.config.js file: an application named api in cluster mode with a process for each core, a restart above 300M of memory, and two sets of environment variables. env applies by default, and env_production is selected with --env production. The environment variables in the file take precedence over the shell variables.
-
pm2 start ecosystem.config.jsStarts all the applications defined in the file.
-
pm2 start ecosystem.config.js --env productionStarts the applications with the environment variables from env_production. After --env, you write the name that follows env_.
-
pm2 start ecosystem.config.js --only apiActs on only one application from the file. --only also works with restart, stop, and delete.
-
pm2 reload ecosystem.config.jsReloads all the applications in the file. restart, stop, and delete work the same way.
Starting automatically when the server boots
-
pm2 startupDetects the server's init system, for example systemd, and prints a command that sets up PM2 to start automatically. Run pm2 startup without sudo, and then copy and run the printed command, which starts with sudo.
-
pm2 startup systemd -u deploy --hp /home/deploySets up automatic startup for a specific user: the -u option is the name of the user who runs the applications, and the --hp option is that user's home directory.
-
pm2 saveSaves the current process list, so PM2 starts the processes again after the server reboots. Run it again every time you add or delete a process.
-
pm2 resurrectManually starts the processes saved the last time you ran pm2 save.
-
pm2 unstartupDisables and removes the automatic startup setup. After a Node.js upgrade, run pm2 unstartup and then pm2 startup, so the automatic startup uses the new version.
No matching commands.
The difference between the process management commands matters on a production server. pm2 restart stops the process and starts it again, so the application is briefly unavailable. pm2 reload replaces the processes one after another in cluster mode, without downtime. pm2 stop stops the process and keeps it in the list, and pm2 delete removes it from the list completely. In cluster mode, each process has its own memory, so data such as user sessions is stored in a database or in Redis, which all the processes share, and not in the memory of the process itself.
You install PM2 as a global npm package. When the installation fails with a permissions error, the way to move global installations to a directory in your home directory is explained on the npm commands page. Run PM2 and the applications as a regular user, not as root, and the pm2 startup command sets up automatic startup for that same user.
A Node.js application on a production server usually listens on a local port, for example 3000, and an Nginx server in front of it receives the requests from the internet, handles the SSL certificate, and passes them to the application (reverse proxy) with the proxy_pass directive. For an application that uses WebSocket, you also add the Upgrade and Connection headers. Ready-made configurations are on the Nginx cheat sheet page.
On a Node.js VPS, the environment is ready in advance: Node.js comes preinstalled and configured, and you get full root access over SSH, so you install PM2 and start your application right away. On a Linux VPS, you connect over SSH with full root access, install Node.js and PM2, and run every command on this page.
PM2 commands FAQ
What's the difference between pm2 restart and pm2 reload?
The pm2 restart command stops the process and starts it again, so the application is briefly unavailable. For an application running in cluster mode, the pm2 reload command replaces the processes one after another, so there's always a process that answers requests, and users don't notice the switch. That's why you use pm2 reload when you deploy a new version of a live application.
How does my application start automatically after the server reboots?
Run pm2 startup, and then copy and run the command it prints, so PM2 starts together with the server. Next, run pm2 save, which saves the list of applications, and PM2 starts them again on every reboot. After you add or delete an application, run pm2 save again.
Does PM2 use all the CPU cores?
Yes, in cluster mode. The pm2 start app.js -i max command starts a separate process for each core, and PM2 distributes the requests among them. This way, a Node.js application uses all the cores of the server, and you can also deploy a new version without downtime with the pm2 reload command.
How do I keep PM2 logs from taking up too much space?
Install the pm2-logrotate module with the pm2 install pm2-logrotate command. The module rotates the log files when they reach the size you set and once a day, and keeps a fixed number of old files. You set the size, the number of files, and compression with the pm2 set command.
Why isn't a new environment variable picked up after pm2 restart?
PM2 keeps the environment variables a process had when it was started. To load new values, add the --update-env option to the command, for example pm2 restart api --update-env. When the variables are defined in an ecosystem file, restart from the file and choose a set of variables with --env.