How to set up a PM2 daemon process manager for a Node.js application?

Reading Time: 9 minutes
PM2 daemon process manager for a Node.js application

Process management encompasses a wide range of activities centered on the creation, termination, and monitoring of processes. A process manager is a program that ensures that your applications remain operational after they have been launched.

Process managers can prevent production downtime by restarting your application automatically after a crash or even after the host machine reboots. They are also useful in development because they restart an app automatically when its source files or dependencies are updated. Monitoring tools that access application logs and other key metrics, such as CPU and memory usage, are also typically provided by process managers.

In this blog, we will cover:

  • What is PM2?
  • Features of PM2
  • Required Installations for the process
  • Hands-on
  • Conclusion

What is PM2?

PM2 is a Node.js process manager with an integrated load balancer. It makes production deployments easier and allows you to keep running applications running indefinitely (even when accidents occur). It also provides insights into your application’s runtime performance and resource consumption, as well as the ability to scale your application in real-time via its clustering feature.

PM2 has additional features such as Docker integration, a JavaScript API, and a daemon-less mode, so make sure to read PM2’s documentation to learn more about these advanced features.

Features of PM2

Features of PM2

Required installations for the process

PM2 daemon process manager for a Node.js application
  • Node.js: It is a JavaScript runtime environment that executes JavaScript code outside the browsers.
  • PM2: It is a daemon process manager that helps in managing and keeping the application online.

Hands-on

In this hands-on, we will see how we can set up a PM2 daemon process manager and manage a Node.js application efficiently keeping it online. We will begin with creating a new directory on the local machine. Open the git bash editor, we will then install the PM2 package into the directory. Opening up the directory in a code editor, we will create a new js file in it. Then, we will add some code in the js file to start the server on the specific hostname and port number and print some content on the UI. We will then start a PM2 process using the shown command to start the server. On success, we will check the output of the application on the browser by hitting the URL on the defined hostname and port number. We will then make some changes in the UI content of the code and use a command to restart the PM2 process. Verifying the same, we will again make some changes in the code base and reload the PM2 instance. Following the same, we will then run commands to stop the application, list the PM2 processes with their statuses, print logs of an application executed on the PM2 process, start/stop an application on system bootup, run the application in a cluster mode, scale up the application to run on multiple processes, monitor the instance, make use of PM2 UI dashboard, and finally delete all the processes of PM2.

Create a new directory on your local machine.

Right-click in the directory and click on git bash.

How to set up a PM2 daemon process manager for a Node.js application?

Execute the command:

npm install pm2

On success, you will see the screen as shown in the image below.

How to set up a PM2 daemon process manager for a Node.js application?

Open any of the code editors.

Open the newly created directory in the code editor.

In the file structure pane, click on New File.

Enter a name for the file and hit enter.

Import the HTTP package using the below code.

Define the hostname and the port number on which you wish to run the server.

Create a new variable thereby creating a server to print the output on the screen and add a status code for the same declaring the content type.

Using the below code, the server will listen to the defined port and host and execute our application on the same URL.

By combining the entire code base, you will have the entire code as shown in the image below.

How to set up a PM2 daemon process manager for a Node.js application?

Now, to start the PM2 server, use the command:

pm2 start app.js

On successful execution of the above command, you will notice the process will be started on a port thereby displaying the status, CPU utilization, memory occupied, etc.

How to set up a PM2 daemon process manager for a Node.js application?

Open any of the browsers, use the URL as localhost:80, and hit enter.

On success, you will see the output on the screen on correct execution.

Now, change the content that is to be printed on the UI.

How to set up a PM2 daemon process manager for a Node.js application?

To display the latest content on the UI, run the command:

pm2 restart app.js

On success, you will see the server running as shown in the image below.

Once you refresh the page, you’ll see the new content on the UI.

Now, navigate to the code file and update the code again with the new content.

How to set up a PM2 daemon process manager for a Node.js application?

To reload the page, run the command:

pm2 reload app.js

Now, you need not refresh the page and you’ll see the new content updated on the UI.

To stop the server, execute the command:

pm2 stop app.js

On success, you will see that the server will stop. The status will change from online to stopped and the CPU utilization and memory will change to 0 since the server has been stopped.

How to set up a PM2 daemon process manager for a Node.js application?

To check the list of PM2 servers being online or offline, run the command:

pm2 list

On executing the above command, you will see the list of applications with their corresponding statuses.

To view the logs for the application, execute the command:

pm2 logs

On success, you will see all the logs for the application you executed with its process id and starting and stopping the application, and many more details.

How to set up a PM2 daemon process manager for a Node.js application?

To keep monitoring a running application, run the command:

pm2 monit

On success, you will get to see a dashboard where you can monitor the running applications.

How to set up a PM2 daemon process manager for a Node.js application?

To use the command services of the PM2 server on a separate dashboard, run the command:

pm2 plus

On success, you will see a URL on the console.

You will also be navigated to a dashboard in your browser using the same URL as shown in the image below. You can hereby perform 3rd logins and use the same command services via a dashboard.

How to set up a PM2 daemon process manager for a Node.js application?

To run the application as soon as you turn on your system, run the command:

pm2 startup

The stop the running of the application on system startup, run the command:

pm2 unstartup systemd

To run the application into a cluster mode, execute the command:

pm2 start app.js -i 3 

(3 – is the number of clusters you wish to have for your application)

On success, you will see the application will be started but the CPU utilization and memory usage will increase since we are now running the application on 3 clusters and the status of the application will change to online.

How to set up a PM2 daemon process manager for a Node.js application?

To scale up the application, execute the command:

pm2 scale app 4

On success, you will see the application execution scaled up to 4 processes.

To stop the execution of the application, execute the command:

pm2 stop app

On success, you will see that the execution of the application will have the status changed to stopped with nullified CPU utilization and memory usage.

How to set up a PM2 daemon process manager for a Node.js application?

To delete the processes and the instances of the application from the PM2 server, run the command:

pm2 delete app

On success, you will see the list will become empty.

Now, you can cross-verify the same running the command:

pm2 list

You will get the same result since you have deleted all the instances of the execution of the application.

Conclusion

In this hands-on, we saw how we can set up a PM2 daemon process manager and manage a Node.js application efficiently keeping it online. We began with creating a new directory on the local machine. Opening up the git bash editor, we then installed the PM2 package into the directory. Opening up the directory in a code editor, we created a new js file in it. Then, we added some code in the js file to start the server on the specific hostname and port number and print some content on the UI. We then started a PM2 process using a command to start the server. On success, we checked the output of the application on the browser hitting the URL on the defined hostname and port number. We then made some changes in the UI content of the code and used a command to restart the PM2 process. Verifying the same, we again made some changes in the code base and reloaded the PM2 instance. Following the same, we then executed commands to stop the application, list the PM2 processes with their statuses, printed logs of an application executed on a PM2 process, start/stop an application on system bootup, run the application in a cluster mode, scale up the application to run on multiple processes, monitor the instance, make use of PM2 UI dashboard, and finally delete all the processes of PM2. We will come up with more such use cases in our upcoming blogs.

Meanwhile…

If you are an aspiring Node Developer and want to explore more about the above topics, here are a few of our blogs for your reference:

Stay tuned to get all the updates about our upcoming blogs on the cloud and the latest technologies.

Keep Exploring -> Keep Learning -> Keep Mastering 

At Workfall, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you’re looking to work with global clients, build cutting-edge products and make big bucks doing so, give it a shot at workfall.com/partner today!

Back To Top