Keep Your App Up 24/7 Using PM2

Agis Septiyanto
Product Monday
3 min readJan 2, 2023

--

PM2 is a process manager that can keep other applications running as long as the computer is on. Applications can run automatically every time the computer is turned on so we don’t have to start the start process manually. Even if the application error or crashes, PM2 will restart the application.

Photo by Pankaj Patel on Unsplash

PM2 is built using the JavaScript language and needs to run using NodeJS. So if you want to use PM2 we need to install NodeJS first. In this article, we will not discuss how to install NodeJS. So, make sure you have already installed NodeJS.

PM2 can be installed using NPM or yarn in this way:

$ npm install pm2@latest -g
# or
$ yarn global add pm2

After PM2 is installed, we can run the pm2 command and the result will appear like this:

To run an application using PM2, it’s quite easy, just run the command:

pm2 start app_name

Examples like this:

pm2 start index.js

Then the running application will appear in the list:

PM2 is not limited to JavaScript applications, but can also be used for various applications. For example, python:

To make applications that run on PM2 start automatically when the computer starts up, we need to create a startup script, PM2 has prepared a command to generate a start-up script, the command is:

pm2 startup

The command above will produce an output start-up and that script must be run:

sudo env PATH=$PATH:/home/agis/.nvm/versions/node/v14.17.6/bin /home/agis/.nvm/versions/node/v14.17.6/lib/node_modules/pm2/bin/pm2 startup systemd -u agis --hp /home/agis

After the above command is executed, so that the process list can be frozen for auto respawn, we can use the command:

pm2 save

And now your application can run automatically in the background. Overall PM2 in my opinion is very helpful, especially for people who need to run several applications in parallel when turning on the computer. This can save time when you want to start working with multiple applications.

--

--