Hello, friends. In this post, you will learn how to manage Node applications using PM2. Although many developers should already know that thanks to PM2 we will be able to control the application we have already made using NodeJS.
Introducing to PM2
According to the PM2 website
PM2 is a daemon process manager that will help you manage and keep your application online 24/7.
So, the main function of PM2 is to manage (start, stop, check status) of an application made in Node.
In addition to this, it includes a function that allows us to monitor the operation of the application. Where we can check logs, and metadata of the same.
PM2 is a free library capable of handling huge amounts of traffic with really low resource consumption. This makes it ideal and necessary for our projects to run smoothly on any server.
So let’s go for it. After this post, you should have the basics on how to use PM2 and manage your Node application.
Install PM2 on the system
PM2 requires Node to be used and installed. So, the first step is to install NodeJS on Linux.
After this, with the help of npm
you can install it
sudo npm install pm2 -g
This is enough to start using PM2 on the system.
Managing a node applications with pm2
To start a NodeJS application using PM2 you have to run
pm2 start [initial-project-file]
For example
pm2 start app.js
A useful feature of PM2 is that we can assign a name to the process we are starting with the application.
To do this, you have to follow this syntax
pm2 start app.js --name "my-app"
This way when we have to do an operation related to our process.
One thing you have to keep in mind is that if you didn’t explicitly assign the process a name when you do the pm2 start
with the --name
option, it will be assigned a name anyway.
If you want to stop the process of your application you have to run
pm2 stop [process-name]
Or restart it:
pm2 restart [process-name]
Also, you can remove it from the process log.
pm2 delete [process-name]
Other PM2 functions
If you run the following command you will get a list of all active applications
pm2 list
In addition to this, some useful information about them is included such as app name
, mode
, ID
, PID
. You can also find out how long it has been running and how much memory has been used.
In the restart
column you can find a counter with the number of times the process has been restarted. If the number increases, it is a sign that something is wrong with the application because PM2 has had to restart it.
This command is quite useful but it does not show us what is happening internally with the application. For this, it is important to consult the logs
.
pm2 log
This command will show you the latest logs and will remain active, showing new messages that the processes send as console output.
Another interesting function is to specify the number of lines for when we have too many applications.
pm2 logs --lines 200
In this case, it will only show up to 200 lines. Feel free to modify this value.
Also, you can check the resources (memory, CPU usage) being handled by the applications. This is possible with the command
pm2 monit
That’s how easy it is to use PM2.
Upgrade PM2 to the latest stable version
It is always important to have the latest stable version of PM2 in order not to miss out on new features. To do this, you have to run
npm install pm2@latest -g
And then,
pm2 update
to perform a memory update.
So, with this, you have the basic PM2 utilities but remember that there are still many more.
Conclusion – Node application with PM2
PM2 is a working tool for managing Node applications and in this post, you got a quick reference on how to use it in a basic way.
So, enjoy it.