Process managers for Express apps

When you run Express apps for production, it is helpful to use a process manager to achieve the following tasks:

Restart the app automatically if it crashes.

Gain insights into runtime performance and resource consumption.

Modify settings dynamically to improve performance.

Control clustering.

A process manager is somewhat like an application server: it’s a “container” for applications that facilitates deployment, provides high availability, and enables you to manage the application at runtime.

The most popular process managers for Express and other Node.js applications are as follows:

Using any of these four tools can be very helpful, however StrongLoop Process Manager is the only tool that provides a comprehensive runtime and deployment solution that addresses the entire Node.js application life cycle, with tooling for every step before and after production, in a unified interface.

Forever

Forever is a simple command-line interface tool for ensuring that a given script runs continuously (forever). Forever’s simple interface makes it ideal for running smaller deployments of Node.js apps and scripts.

Installation

Basic use

To start a script, use the forever start command and specify the path of the script:

$ forever start script.js

This command will run the script in daemon mode (in the background).

To run the script so that it is attached to the terminal, omit start:

$ forever script.js

It is a good idea to log output from the Forever tool and the script by using the logging options -l, -o, and -e, as shown this example:

$ forever start -l forever.log -o out.log -e err.log script.js

To view the list of scripts that were started by Forever:

$ forever list

To stop a script that was started by Forever use the forever stop command and specify the process index (as listed by the forever list command).

$ forever stop 1

Alternatively, specify the path of the file:

$ forever stop script.js

To stop all the scripts that were started by Forever:

$ forever stopall

Forever has many more options, and it also provides a programmatic API.

PM2

PM2 is a production process manager for Node.js applications, that has a built-in load balancer. PM2 allows you to keep applications alive forever and reload them without downtime, and will facilitate common system admin tasks. PM2 also enables you to manage application logging, monitoring, and clustering.

When you start an app by using the pm2 command, the app is immediately sent to the background. You can control the background app from the command line by using various pm2 commands.

After an app is started by using the pm2 command, it is registered in PM2’s list of processes with an ID. You can therefore manage apps with the same name from different directories on the system, by using their IDs.

Note that if more than one app with the same name is running, pm2 commands affect all of them. So use IDs instead of names to manage individual apps.

List all running processes:

$ pm2 list

Stop an app:

$ pm2 stop 0

Restart an app:

$ pm2 restart 0

To view detailed information about an app:

$ pm2 show 0

To remove an app from PM2’s registry:

$ pm2 delete 0

StrongLoop Process Manager

StrongLoop Process Manager (StrongLoop PM) is a production process manager for Node.js applications. StrongLoop PM has built-in load balancing, monitoring, and multi-host deployment, and a graphical console. You can use StrongLoop PM for the following tasks:

Build, package, and deploy your Node.js application to a local or remote system.