Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime, and to facilitate common system admin tasks.
Process Management
Start an application with PM2 and keep it running in the background.
pm2 start app.js
Load Balancing
Enable load balancing by starting multiple instances of the application across all CPUs.
pm2 start app.js -i max
Monitoring
Monitor all processes launched with PM2 using a terminal-based dashboard.
pm2 monit
Logging
Display logs of all processes in real-time, or target specific processes.
pm2 logs
Startup Script
Generate a startup script to resurrect PM2 and all processes on server reboot.
pm2 startup
Update Processes
Reload all processes in the cluster without downtime.
pm2 reload all
Forever is a simple CLI tool for ensuring that a given script runs continuously (i.e., forever). It is similar to PM2 but lacks some of its advanced features like application monitoring and load balancing.
Nodemon is a utility that monitors for any changes in your source and automatically restarts your server. It is often used in development environments, unlike PM2 which is tailored for production use.
StrongLoop Process Manager (strong-pm) is a production process manager for Node.js applications with features like remote deployment and performance monitoring. It is part of the StrongLoop suite, which is now owned by IBM.
Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. It is not specifically designed for Node.js and is more general-purpose.
pm2 is a process manager for Node apps with a builtin load-balancer.
pm2 is perfect when you need to spread your stateless code accross all CPUs available on a server, to keep all processes alive forever and to 0s reload it.
Tested with Node v0.11, v0.10, v0.8 (https://travis-ci.org/Unitech/pm2). Compatible CoffeeScript. Works on Linux & MacOS.
npm install -g pm2
# Usage/Features
$ npm install pm2 -g # Install pm2 command line globally
$ pm2 start app.js -i 4 # Daemonize pm2 and Start 4 clustered instances of app.js
# You can also pass the 'max' params to start
# the right numbers of processes depending of CPUs
$ pm2 start app.js --name my-api # Name process
$ pm2 list # Display all processes status
$ pm2 monit # Monitor all processes
$ pm2 logs # Display all processes logs in streaming
$ pm2 stop all # Stop all processes
$ pm2 restart all # Restart all processes
$ pm2 reload all # Will 0s downtime reload (for NETWORKED processes)
$ pm2 stop 0 # Stop specific process id
$ pm2 restart 0 # Restart specific process id
$ pm2 startup # Generate init script to keep processes alive
$ pm2 web # Launch Health computer API endpoint (http://localhost:9615)
$ pm2 delete 0 # Will remove process from pm2 list
$ pm2 delete all # Will remove all processes from pm2 list
For other nature scripts :
$ pm2 start echo.php
$ pm2 start echo.py
$ pm2 start echo.sh
$ pm2 start echo.rb
$ pm2 start echo.pl
## Different ways to launch a process
$ pm2 start app.js -i max # Will start maximum processes depending on CPU availables
$ pm2 start app.js -i 3 # Will start 3 processes
$ pm2 start app.js -x # Start app.js in fork mode instead of cluster
$ pm2 start app.js -x -- -a 23 # Start app.js in fork mode and pass arguments (-a 23)
$ pm2 start app.js --name serverone # Start a process an name it as server one
# you can now stop the process by doing
# pm2 stop serverone
$ pm2 start app.json # Start processes with options declared in app.json
# Go to chapter Multi process JSON declaration for more
$ pm2 start app.js -i max -- -a 23 # Pass arguments after -- to app.js
$ pm2 start app.js -i max -e err.log -o out.log # Will start and generate a configuration file
You can also execute app in other languages (the fork mode):
$ pm2 start my-bash-script.sh -x --interpreter bash
$ pm2 start my-python-script.py -x --interpreter python
## 0s downtime reload
This feature permits to reload code without loosing queries connection.
Warning :
$ pm2 reload all
Thanks to TruongSinh Tran-Nguyen https://github.com/truongsinh
## CoffeeScript$ pm2 start my_app.coffee
That's all !
## Fork mode - execute script in different languagesThe default mode of PM2 consists of wrapping the code of your node app into the Node Cluster module. It's called the cluster mode. There is also a more classical way to execute your app, like node-forever do, called the fork mode.
In fork mode all options are the same than the cluster mode (restart, delete...).
By using the fork mode you will loose core features of PM2 like the automatic clusterization of your code over all CPUs available and the 0s reload.
So use it if you only need a forever like behaviour.
Here is how to start your app in fork :
$ pm2 start app.js -x # Will start your app.js in fork mode
$ pm2 list # You will see that on the row "mode" it's written "fork"
You can also exec scripts in other languages :
$ pm2 start my-bash-script.sh -x --interpreter bash
$ pm2 start my-python-script.py -x --interpreter python
## Is my production server ready for PM2 ?
Just try the tests before using PM2 on your production server
$ 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 broken please report us issues here
## pm2 listList infos about all processes managed by pm2. It shows also how many times a process has been restarted because of an unhandled exception.
## pm2 monitMonitor CPU and memory usage of every node process (and also clustered processes) managed by pm2.
## pm2 automatic startup script generationPM2 provides an automatic way to keep Node processes alive on server restart. On exit it will dump the process list and their environment and will resurrect them on startup. It uses System V init script compatible with Ubuntu and CentOS (maybe it works on other sys but not 100% sure).
$ pm2 startup # then follow the command instruction
The -u username
option permits to specify which user has to start the process at startup.
NOTE that this user must have access to npm, apps and node ! So the best way is to log with this user su -l www
, then do pm2 startup -u www
.
Internally it uses sudo -u $USER
.
Display logs in streaming of all processes, without having to do a tail -f or something else. You can also pass [name|id] as parameter to stream only the log of a specified process.
## pm2 health web api endpointPM2 can disserve an API endpoint to monitor processes and computer health (cpu usage, memory, network interfaces...)
pm2 web
## Customization
Multiple variables can be customized via the environment :
DAEMON_BIND_HOST : process.env.PM2_BIND_ADDR || 'localhost',
DAEMON_RPC_PORT : process.env.PM2_RPC_PORT || 6666, // RPC commands
DAEMON_PUB_PORT : process.env.PM2_PUB_PORT || 6667, // Realtime events
DEBUG : process.env.PM2_DEBUG || false,
WEB_INTERFACE : process.env.PM2_API_PORT || 9615,
# Multi process JSON declaration
processes.json :
[{
"name" : "echo",
"script" : "./examples/args.js",
"args" : "['--toto=heya coco', '-d', '1']",
"exec_mode" : "fork_mode"
},{
"name" : "api",
"script" : "./examples/child.js",
"instances" : "4",
"error_file" : "./examples/child-err.log",
"out_file" : "./examples/child-out.log",
"exec_mode" : "cluster_mode",
"port" : 9005
},{
"min_uptime" : "100",
"max_restarts" : "400",
"name" : "auto-kill",
"script" : "./examples/killfast.js"
}]
Then with the cli :
$ pm2 start processes.json
Notes
"port" : 9005
is present in the process environment"min_uptime": if a process is restarted with an uptime smaller than this value, this restart counts as an unstable restart. If this option is not specified, all restarts are considered unstable.
"max_restarts": if the number of unstable restarts exceeds this number, the process will be stopped and a message with number with restarts will be logged.
Fork PM2 and to hack it it's pretty simple :
$ pm2 kill # kill the current pm2
$ git clone my_pm2_fork.git
$ cd pm2/
$ DEBUG=* PM2_DEBUG=true./bin/pm2 start xxx.js
Everytime you do a modification on the code you have to restart pm2, so just do a ./bin/pm2 kill
before
starting an app or something else.
You have to restart it because the code is daemonized on the memory.
First, install the lastest pm2 version :
$ npm install -g pm2@latest
$ pm2 start index.js -x # start my app in fork mode
For more informations about this issue : #74
Cannot read property 'getsockname' of undefined
When using the cluster mode (by default) you can't use ports from 0 to 1024. If you really need to exec in this range use the fork mode with the -x
parameter.
By using the fork mode you will loose core features of PM2 like the automatic clusterization of your code over all CPUs available and the 0s reload.
npm test
# They talk about it
Thanks to Devo.ps and Wiredcraft for their knowledge and expertise.
# License - Apache License v2Copyright [2013] [Strzelewicz Alexandre as@unitech.io]
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
FAQs
Production process manager for Node.JS applications with a built-in load balancer.
The npm package pm2 receives a total of 1,910,388 weekly downloads. As such, pm2 popularity was classified as popular.
We found that pm2 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.