Recently, my company started a personal project and I was assigned a task to select a server-side framework that fits our needs. Looking and testing out various frameworks, I finally settled on NestJs. I was really impressed by the Code-Quality, Documentation and Versatility it had to offer us. Moreover, Node.js and Typescript were just icing on the cake.
Having developed the application. Finally, we had to deploy it to the server and there came a problem. We had to keep the Node.js process running for forever in the background. We came out with various solutions to this issue but nothing seemed to be idle. Until we discovered a library called PM2 (Process Manager 2) and we instantly fell in love with it.
PM2 (Process Manager 2)
PM2 (Process Manager 2) is a daemon process manager that will help you manage and keep your application online. It’s free, open-source, advance, versatile and cross-platform (Mac, Linux & Windows) production level process manager written in Node.js. Moreover, PM2 isn’t just limited to Node.js but also you’re free to manage the processes of other languages or scripts using the same tool.
Use Cases Of PM2 (Process Manager 2)
There are many ways in which PM2 can be used. Few of which are given below:
- Running process for forever: PM2’s primary goal is to run the process in the background for forever (Until your server goes down).
- Restarting process after a server crash or failure: That’s right, PM2 can automatically start your process on the restart of the server every time so you don’t have to.
- Managing or monitoring logs: PM2 can preserve logs of an individual process so you can view them anytime in the future.
I assume that you have already installed Node.js and NPM into the system. Next steps are easy and can get you started with PM2 in no time. To install PM2 into the system you just have to run one command. Basically, you can use both NPM or Yarn to install PM2.
npm install [email protected] -g #OR yarn global add pm2
To start your app or to run the process in the background you need to use the commands provided by PM2. The simplest way to start, daemonize and monitor your application is by entering the command given below.
# <app> being the filename you want to run pm2 start <app> # OR pm2 start "npm run start"
Once you run the command given above PM2 will start running your app or process in the background and will provide you with an id of the process which you can use to monitor, restart, stop or do various other things with that process. You can also use the
--name flag and give it a value in the command to assign the name to the process in case you don’t want to remember the id of the process.
pm2 --name my-app start app
Or, in case you want to see all the processes being managed by PM2.
Or, maybe if you’d like to view the logs of the process. You can supply either supply the
id of the process to the
pm2 logs my-app
Moroever, You can also ask PM2 to restart the process if it notices any changes in the file using the flag
--watch in the
pm2 --name my-app --watch start app
Starting process on System (re)start or failure
Certain times we need to restart the system. I bet you wouldn’t want to go through all the hustle above if know that you can automate this task and PM2 makes it really easy. All we need to do is save our processes with PM2 and let it handle the rest. For which we use the
save command provided to us by PM2. Follow the commands given below to make yourself happy.
# Automatically starts up PM2 on the system start pm2 startup # Saves the list of processes to start automatically on system start pm2 save
There are various other things PM2 has to offer and there isn’t a better place than its documentation itself. Go checkout PM2 documentation for more and let me know how did it help you in the comments below.