# PM2 Documentation > PM2 is a production process manager for Node.js applications with a built-in > load balancer. It keeps applications alive forever, reloads them without > downtime, manages logs, and facilitates common system admin tasks. # Quick Start Source: https://pm2.keymetrics.io/docs/usage/quick-start/ ## PM2 Process Management 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: ```bash $ npm install pm2@latest -g # or $ yarn global add pm2 ``` To install Node.js and NPM you can use [NVM](https://yoember.com/nodejs/the-best-way-to-install-node-js/) ### Start an app The simplest way to start, daemonize and monitor your application is by using this command line: ```bash $ pm2 start app.js ``` Or start any other application easily: ```bash $ pm2 start bashscript.sh $ pm2 start python-app.py --watch $ pm2 start binary-file -- --port 1520 ``` Some options you can pass to the CLI: ```bash # Specify an app name --name # Watch and Restart app when files change --watch # Set memory threshold for app reload --max-memory-restart <200MB> # Specify log file --log # Pass extra arguments to the script -- arg1 arg2 arg3 # Delay between automatic restarts --restart-delay # Prefix logs with time --time # Do not auto restart app --no-autorestart # Specify cron for forced restart --cron # Attach to application log after start --attach # Run the PM2 daemon in the foreground --no-daemon ``` 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: ```bash $ 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 managed applications List the status of all application managed by PM2: ```bash $ pm2 [list|ls|status] ``` ![pm2 ls output listing managed Node.js processes](/images/docs/pm2-ls-output.png) ### Display logs To display logs in realtime: ```bash $ pm2 logs ``` To dig in older logs: ```bash $ pm2 logs --lines 200 ``` ### Terminal Based Dashboard Here is a realtime dashboard that fits directly into your terminal: ```bash $ pm2 monit ``` ![pm2 monit terminal-based monitoring dashboard](/images/docs/pm2-monit-dashboard.png) ### pm2.io: Monitoring & Diagnostic Web Interface Web based dashboard, cross servers with diagnostic system: ```bash $ pm2 plus ``` ![PM2 Plus web monitoring dashboard](/images/docs/pm2-plus-dashboard.png) ## 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: ``` $ pm2 start app.js -i max ``` Read more about cluster mode [here](/docs/usage/cluster-mode/). ## Ecosystem File You can also create a configuration file, called Ecosystem File, to manage multiple applications. To generate an Ecosystem file: ```bash $ pm2 ecosystem ``` This will generate an ecosystem.config.js file: ```javascript 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: ```bash $ pm2 start ecosystem.config.js ``` Read more about application declaration [here](/docs/usage/application-declaration/). ## 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: ```bash $ pm2 startup ``` And to freeze a process list for automatic respawn: ```bash $ pm2 save ``` Read more about startup script generator [here](/docs/usage/startup/). ## Restart application on changes It's pretty easy with the `--watch` option: ```bash $ 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. ## Updating PM2 We made it simple, there is no breaking change between releases and the procedure is straightforward: ```bash npm install pm2@latest -g ``` Then update the in-memory PM2 : ```bash 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: ```bash # 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 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 information 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 # 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 ``` ## What's next? Learn how to declare all your application's behavior options into a [JSON configuration file](https://pm2.keymetrics.io/docs/usage/application-declaration/). Learn how to do [clean stop and restart](https://pm2.keymetrics.io/docs/usage/signals-clean-restart/) to increase reliability. Learn how to [deploy and update production applications easily](https://pm2.keymetrics.io/docs/usage/deployment/). Monitor your production applications with [PM2.io](https://app.pm2.io/). ## How to update PM2 Install the latest pm2 version: ```bash npm install pm2@latest -g ``` Then update the in-memory PM2 : ```bash pm2 update ``` --- # Process Management Source: https://pm2.keymetrics.io/docs/usage/process-management/ ## Managing applications states With PM2 you can easily start/restart/reload/stop/list applications in background. ### Start To start an application: ```bash $ pm2 start api.js ``` ![pm2 stop command output](/images/docs/pm2-stop.png) You can also start any kind of application like bash commands, script, binaries: ```bash $ pm2 start "npm run start" $ pm2 start "ls -la" $ pm2 start app.py ``` #### Start and display log stream To start an app and check logs stream use the `--attach` option: ```bash $ pm2 start api.js --attach ``` When quitting via Ctrl-C, the app will still run in background. #### Passing arguments All option passed after `--` will be passed as argument to the app: ```bash $ pm2 start api.js -- arg1 arg2 ``` #### Configuration File When managing multiple application at the same time or having to specify multiple options, you can use a configuration file. Example with this ecosystem.config.js file: ```javascript module.exports = { apps : [{ name : "limit worker", script : "./worker.js", args : "limit" },{ name : "rotate worker", script : "./worker.js", args : "rotate" }] } ``` Then to start both apps: ```bash $ pm2 start ecosystem.config.js ``` Read more about [configuration file](/docs/usage/application-declaration/). ### Restart To restart an application: ```bash $ pm2 restart api ``` To restart all applications: ```bash $ pm2 restart all ``` To restart multiple apps at once: ```bash $ pm2 restart app1 app3 app4 ``` #### Updating environment variables and options To update environment variables or PM2 options, specify the `--update-env` CLI option: ```bash $ NODE_ENV=production pm2 restart web-interface --update-env ``` ### Stop To stop a specified application: ```bash $ pm2 stop api $ pm2 stop [process_id] ``` To stop them all: ```bash $ pm2 stop all ``` To stop multiple apps at once: ```bash $ pm2 stop app1 app3 app4 ``` Note: this will not delete the application from PM2 application list. See next section to delete an application. ### Delete To stop and delete an application: ```bash $ pm2 delete api ``` To delete them all: ```bash $ pm2 delete all ``` ## Listing Applications To list all running applications: ```bash $ pm2 list # Or $ pm2 [list|ls|l|status] ``` ![pm2 restart command output](/images/docs/pm2-restart.png) To specify which order you want the application to be listed: ```bash $ pm2 list --sort name:desc # Or $ pm2 list --sort [name|id|pid|memory|cpu|status|uptime][:asc|desc] ``` ### Terminal Dashboard PM2 gives you a simple way to monitor the resource usage of your application. You can monitor memory and CPU easily and straight from your terminal with: ```bash pm2 monit ```
pm2 monit terminal dashboard
### Showing application metadata To display metadata about an application: ```bash $ pm2 show api ``` drawing ### Reset restart count To reset the restart counter: ```bash $ pm2 reset all ``` --- # CLI Reference Source: https://pm2.keymetrics.io/docs/usage/commands/ ## PM2 CLI Command Reference Every PM2 command, one line each, grouped by theme. Run `pm2 -h` to list the options of a specific command, or `pm2 examples` to display common usage examples. ### Process Control | Command | Description | |:--------|:------------| | `pm2 start ## What is the difference between fork mode and cluster mode? **Fork mode** runs your application as a single process, exactly like running it with `node` directly. **Cluster mode** spawns multiple instances of your Node.js application and load-balances incoming HTTP/TCP connections between them using the Node.js cluster module — using all available CPU cores with zero code change: ```bash pm2 start app.js -i max ``` Cluster mode only works for Node.js applications; scripts in other languages run in fork mode. Read more in the [cluster mode documentation](/docs/usage/cluster-mode/). ## How do I make PM2 restart my apps automatically after a server reboot? Generate an init script for your platform, run the command it prints, then freeze your process list: ```bash pm2 startup # run the command printed by pm2 pm2 save ``` PM2 and your applications will be resurrected at boot. See the [startup script documentation](/docs/usage/startup/). ## Where are the PM2 log files located? Application logs live in `$HOME/.pm2/logs`, one output and one error file per process. ```bash pm2 logs # stream logs in realtime pm2 flush # empty all log files pm2 install pm2-logrotate # rotate logs automatically ``` More in the [log management documentation](/docs/usage/log-management/). ## What is the difference between pm2 restart and pm2 reload? `pm2 restart` kills and restarts the process (brief downtime). `pm2 reload` performs a **zero-downtime reload** for cluster-mode applications: new workers are spawned and old ones are only killed once the new ones are ready to accept connections. ## How do I limit the memory used by an application? ```bash pm2 start app.js --max-memory-restart 300M ``` PM2 checks memory usage every 30 seconds and reloads the process when it exceeds the threshold. Units `K`, `M` and `G` are supported. See [memory limit](/docs/usage/memory-limit/). ## How do I update PM2 to the latest version? ```bash npm install pm2@latest -g pm2 update ``` `pm2 update` replaces the in-memory daemon without losing your process list. ## Can PM2 run applications written in other languages than Node.js? Yes — PM2 can start any script or binary: ```bash pm2 start app.py pm2 start ./binary-file -- --port 1520 ``` The interpreter is picked from the file extension and can be overridden with `--interpreter`. Cluster mode, however, is Node.js only. ## How do I run PM2 inside a Docker container? Use `pm2-runtime`, a drop-in replacement for the `node` binary designed for containers: ```dockerfile RUN npm install pm2 -g CMD ["pm2-runtime", "app.js"] ``` It keeps PM2 in the foreground so the container stays alive. See the [Docker integration documentation](/docs/usage/docker-pm2-nodejs/). ## I can't connect my local PM2 to the PM2.io dashboard If you are in this situation, it might be for several reasons. - You are behind a company proxy or firewall. Make sure the 443 port is opened and/or set the proxy config: `PM2_PROXY= pm2 link ` - You are using a old PM2 version. Update PM2 to latest: `npm install pm2@latest -g` - You have concurrent PM2 sending data to the same bucket with an identical server name. Make sure you have only one PM2 instance launched `ps -aux | grep PM2` - Refresh your connection to PM2.io. `pm2 link stop` then `pm2 link start`. Also don't forget to refresh the dashboard itself, it might help sometimes. ## The versioning buttons (Rollback/Pull/Upgrade) aren't working - If the buttons are disabled, make sure that the `Local changes` and `Local commit` indicators are green. - If you get a warning `Not authorized` when trying to perform such actions, it means you have not the admin privileges in this bucket. - If none of the above happens, but the procedure just hangs, make sure you have a recent version of Node.js as well as the latest version of PM2. - Also, your repository should not ask for a password input (it means you must clone it via ssh), try typing `git remote update` manually in the folder and see if it asks for a password or not. ## How do I group processes together with namespaces? Start processes with a namespace, either from the CLI or with the `namespace` attribute in the [configuration file](/docs/usage/application-declaration/): ```bash pm2 start api.js --namespace backend pm2 start worker.js --namespace backend ``` Every command that accepts a process name also accepts a namespace, so you can act on the whole group at once: ```bash pm2 restart backend pm2 stop backend pm2 logs backend ``` ## How do I get a diagnostic report about my PM2 installation? ```bash pm2 report ``` It prints a full report — PM2, Node.js and OS versions, daemon state and the process list. Attach it when [opening an issue](https://github.com/Unitech/pm2/issues). ## How do I start or reload applications in a CI/CD pipeline? ```bash pm2 startOrReload ecosystem.config.js pm2 save ``` `pm2 startOrReload` starts the applications if they are not running and performs a **zero-downtime reload** if they are, making deployments idempotent: the same command works on the first deploy and on every update. `pm2 startOrRestart` does the same with a hard restart. Follow it with `pm2 save` so the new process list survives a reboot. ## Can I use the PM2 documentation with AI assistants like ChatGPT, Claude or Cursor? Yes — the documentation follows the [llms.txt convention](https://llmstxt.org): - [https://pm2.keymetrics.io/llms.txt](https://pm2.keymetrics.io/llms.txt) — a Markdown index of every documentation page - [https://pm2.keymetrics.io/llms-full.txt](https://pm2.keymetrics.io/llms-full.txt) — the entire documentation in a single Markdown file Paste either URL into your AI tool to give it up-to-date PM2 knowledge. --- # Contributing Source: https://pm2.keymetrics.io/docs/usage/contributing/ ## Contributing/Development mode It's very simple to play with PM2: ```bash pm2 kill # kill the current pm2 git clone my_pm2_fork.git cd pm2/ DEBUG=* ./bin/pm2 --no-daemon ``` Each time you edit the code, be sure to kill and restart PM2 to make changes taking effect. **DEBUG="*"** Allows to display all possible debug logs in ~/.pm2/pm2.log ## Install PM2 development ```bash npm install https://github.com/Unitech/pm2#development -g ``` ## Launch the tests Just try the tests before using PM2 on your production server: ```bash git clone https://github.com/Unitech/pm2.git cd pm2 npm install # Or do NODE_ENV=development npm install if some packages are missing npm test ``` If a test is not correctly working, please report issues [here](https://github.com/Unitech/pm2/issues?state=open). You should also make sure that you have all dependencies needed. For Ubuntu: ```bash sudo apt-get install build-essential # nvm is a Node.js version manager - https://github.com/creationix/nvm wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh nvm install 4 nvm use 4 ```