Back in May 2015 we updated our server setup and started to use a bunch of new tools to avoid server administration headaches. We mostly rely on Node.js apps (Ghost, our homepage is built on hapi, Strider) and wanted a reliable process manager to keep started servers alive. We had various requirements to the prospective process manager: intuitive API which is accessible via command line, automatically restart processes on system reboots, seamless updates, and advanced process configuration including process specific environment variables.
And you as you read in the headline of this tutorial, you already know our tool of choice: PM2. Within this series, we’ll show you the extensive set of PM2’s functionality. Before we move on, let’s have look at the series overview and what to expect within the upcoming weeks.
PM2 Series Overview
- Utility Overview & Installation
- Series Round-Up
What Is PM2?
You can install PM2 with NPM and it automatically integrates with your command line. The installation process is covered later within this article.
PM2 has a rich set of functionalities and we just pointed out some of the features. There are a lot other process managers for Node.js applications like supervisor, forever, etc. We think PM2 has some major advantages over all others: intuitive API accessible via CLI, stability, great console output. That later may not be one of the critical requirements when deciding for a tool to use. Nevertheless, we had multiple situations where PM2’s output saved us a lot headaches and time when searching for process information.
The list below outlines functionality which we think is highly beneficial for every system administrator running Node.js apps in production.
Major Feature Set
- Process management including automatic app restarts on failure or system reboots
- Application monitoring
- Declarative configuration via JSON file
- Log management
- Built-in cluster mode
- Startup script generation for *nix systems
- Seamless updates
- Integrated module system
PM2 has a lot more features which we didn’t listed above. If you already do or decided to use PM2, you’ll recognize its benefits gradually. However, we’ll keep you updated about PM2’s functionality throughout this series :)
Installing PM2 requires NPM to be installed on your machine. NPM gets installed with Node.js and if you didn’t have both tools installed, please download and install Node.js first and proceed from the paragraph below.
We recommend to install PM2 globally for the current system user since you’ll probably use it for multiple Node.js servers and the command line utility integrates seamless into your system shell.
npm install -g pm2
While writing and updating this series, we’ve steadily updated related tutorials. You can rely at least on version
2.2.2 of PM2. Initially, the whole PM2 series was written using
0.14.7 and was continuously updated to version
2.2.2. For the future, we’ll further update the PM2 guides on new releases and breaking changes.
pm2 command should be available via command line and you can check the installed version via
$ pm2 -v 2.2.2
or print out the functionality overview
$ pm2 -h Usage: pm2 [cmd] app Commands: start [options] <file|json|stdin|app_name|pm_id…> start and daemonize an app docker:gen [options] <file|config> generate Dockerfile in current folder … deploy <file|environment> deploy your json … Deployment help: $ pm2 deploy help
PM2’s help output also shows you the general usage of this utility. This part is important to remember and you need to understand the syntax of commands to be executed with
General Usage for the Utility
For almost every PM2 command you’re going to use the same syntax (except get/set config values):
$ pm2 <command> app_name|appfile.js
PM2 expects the command which should be executed as the first parameter and afterwards the app_name or application file as second parameter. We’ll cover the commands to (re)start, stop, delete processes, etcd. later within this series. For the beginning, it’s essential to understand what PM2 is, how to install the utility and how to get help if you can’t remember commands or things went south.
If you aren’t sure about the correct syntax while submitting commands on your server, you can always access and output a helping hand with
What Comes Next
This article helps you getting started with the process manager PM2. We guide you how to install PM2 with the use of NPM and show you the general usage of
pm2 commands from command line.
The next article covers the process management, namely how to start, stop, and delete Node.js processes with PM2.
If you have any questions, please don’t hesitate to contact us via @futurestud_io or use the comments below.