Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 983 of the free egghead.io lessons, plus get Node.js content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Using PM2 To Keep Your Node Apps Alive

5:24 Node.js lesson by

Learn how to install, configure and use pm2, the popular node.js process manager to keep your node apps alive whenever things go wrong.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Learn how to install, configure and use pm2, the popular node.js process manager to keep your node apps alive whenever things go wrong.

Avatar
yskang

could you let me know how to use pm2 like video in windows?

Avatar
Will

Hi yskang!
PM2 on Windows is supported according to the docs. All the commands should be the same. Have a look here for a quick reference guide: http://pm2.keymetrics.io/docs/usage/quick-start/

In reply to yskang

Using pm2 can help keep our node apps alive when things go wrong. Our first step in getting that set up is to SSH into our production server. Once we're logged into our server, we want to go ahead and gain root access. Then we can install the pm2 module using the npm installer.

Once the installation has completed, we need to configure a few things on pm2. To do so, we're going to issue the pm2 command followed by two different arguments.

The first argument is the startup argument, and that just tells pm2 to configure itself for startup and generate the necessary scripts so that it can launch when the system boots. The second argument is centos, or centOS, which tells pm2 the type of Linux distribution we're running on so that it can configure the init.d scripts correctly.

Once that's complete, we get a little bit of output here. The first one tells us that it generated the init scripts and placed them in the /etc/init.d folder. It also set the script so that it will start at bootup.

By default, pm2 configures itself to run as root, and that's perfectly OK and it'll work fine, but it's not considered to be best practice in the Linux community. The general rule to remember is that you never want to run anything on your production servers as root unless it's absolutely necessary. In our case it's not, so we're going to generate a user account that pm2 can use to run.

We'll do that issuing a useradd command, followed by a -s to tell it we want to use the bash shell. Then a -m so that it knows to manage the home directory, which should be placed in /home/node. Finally the name of our user account is going to be node. Now that that's created, we need to modify the init.d scripts that were created.

As we look in the init.d script here, we see two different variables that are of interest to us. The first is the user variable, which specifies the username the script should run as. The second is an exported variable called PM2_HOME which is going to be the folder that pm2 looks in to find the scripts for the applications that it's responsible for managing.

The first thing we'll do is change our username, and we're going to change it to node, which is the name of the user account we created. Then we want to change the PM2_HOME variable to be the home directory of our user account.

Next we want to actually switch to the user account. Inside of our home folder, we just want to create a simple server app. If we take a look at the code here, we can see that it's not doing anything other than just returning "Hello World" to our browser.

We can actually test this now using a method that you've probably used in the past just by calling the node command passing in the name of our JavaScript file. The server's up and running, so if we switch over to our browser, the page is going to load and it's going to return "Hello World," just like we were expecting.

Now let's kill that, and let's do the same thing using our pm2 program. To start it with pm2, we'll call the pm2 command followed by the start argument, and then the name of the file that we would like pm2 to use as our application.

When we press return we get a little bit of output here. The first one says that the pm2 daemon was successfully spawned, and it's going to be monitoring our processes. The second piece of output that we want to look at here is that a process was launched containing our application code from the server.js file.

Next we get this table that shows us the app name, which defaults to the name of the JavaScript file passed as an argument, an ID that pm2 uses to track the application, and the pid, or process ID, that the operating system is using for that application.

If we come back over to our browser again and we reload the page, we get "Hello World" loading just like we did previously. The difference this time is that now pm2 is monitoring our application for us, so if the application stops responding or crashes, which we can actually simulate by issuing a kill -9 followed by our pid. We've killed the process. But if we reload our page, we see we still get "Hello World."

If we finally issue the pm2 list command, we'll get that same table, console table. But this time if we look at the pid, we can see that there's a new pid, indicating that pm2 was able to detect that our application crashed and it started a new process so that the application remained available to our end users.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?