What is PM2?
PM2 is an advanced process manager for NodeJS applications that allows you quickly start, control, or stop your node processes. It runs as a daemon on the server and will make sure your app is available 24/7/365!
Of course, we at HostArmada want to offer our clients the best tools to manage their NodeJS applications. Thus we preinstalled PM2 on all hosting plans. This means our customers can start using it immediately when they purchase our services and have a NodeJS application they want to deploy!
In this article, we will introduce you to the basics of PM2 and teach you how you can use it to control the flow of your node applications! Let's begin.
How to start a node application with PM2
The most straightforward method is by typing the following command over the terminal inside the root directory of your app:
pm2 start app.js
Where "app.js" is your application startup file, this file can have different names so please, adjust the command accordingly.
Here are a few useful options you can add to the command:
--name <name_of_nodejs_app> - You can use this flag to name your application. It is useful when you have more than one app added to the PM2 manager to differentiate them.
--watch - This flag will tell the PM2 manager to restart the NodeJS process whenever it detects a file change.
--log - This flag will allow you to define a log file where PM2 will add entries, should there be any issues with your node app.
--max-memory-restart <enter_value_here> - You can use this option to limit the amount of memory a node app can use. It could be useful when you have a limited amount of RAM at your disposal, and you want to distribute the memory evenly amongst your applications.
There are, of course, more options available. However, we believe that these four above are the most widely used ones. You will find them very handy, depending on your current situation.
How to manage your node processes with PM2?
Managing your processes is as straightforward as starting them. Here are all the commands you need:
pm2 restart <app_name>
Using the above command will restart the PM2 process.
pm2 stop <app_name>
The command listed above will stop the node process.
pm2 delete <app_name>
If you use the above command, PM2 will delete the app and will no longer manage it. If you want to execute the above commands for all your applications added to PM2, replace the <app_name> with <all>. Here is an example:
pm2 restart all
How to list all the processes inside the PM2 manager?
PM2 allows you to view a list of all the running process by executing the command:
When you type it in, you will see a table section that contains the process id, name, version, PID, uptime, status, and resource usage.
How to monitor your apps in real-time using PM2
Perhaps the coolest thing about PM2 is the real-time monitoring option it allows the user. When you execute the command:
You will notice a list of all your processes on the left side of the screen. You can navigate all the node processes using the "Up" and "Down" buttons on your keyboard. On the right side of the screen, you will see the "logging section". There, you can view everything that is going on with the app - both good and bad!
There you have it - an elementary guide for a very user-friendly yet powerful process manager for handing NodeJS applications. We sincerely hope that this guide provided you the needed insight and confidence to start managing your node apps like a pro!