Using PM2 To Keep Your Node Apps Alive

Will Button
InstructorWill Button

Share this video with your friends

Send Tweet
Published 7 years ago
Updated 3 years ago

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

[00:03] 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.

[00:27] 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.

[00:38] 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.

[01:00] 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.

[01:14] 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.

[01:41] 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.

[02:08] 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.

[02:32] 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.

[02:57] 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.

[03:20] 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.

[03:41] 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.

[03:59] 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.

[04:17] 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.

[04:35] 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."

[05:03] 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.

~ 7 years ago

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

Will Button
Will Buttoninstructor
~ 7 years ago

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: