1. Introduction

Making sure that applications automatically launch successfully after a system reboot is essential in the world of server administration and application deployment.

Process Manager 2, or PM2, is a well-liked option among developers and system administrators for managing production applications. It has a variety of useful capabilities, including automatic restarts, load balancing, and, most importantly, the ability to start applications automatically after a system reboot.

In this article, we will explore using PM2 to achieve this seamless reboot for our applications.

2. What Is PM2?

Process Manager 2, as the name suggests, is a process manager for production applications. Chiefly, it helps manage and maintain application processes in production environments. Moreover, PM2 provides a seamless solution to automate starting applications after a system reboot.

By utilizing PM2’s built-in features, we can ensure that our applications automatically launch on reboot, allowing us to focus on other critical aspects of managing our services.

3. How to Install PM2?

Now, let’s proceed with the installation of PM2. The Node.js and npm modules are both necessary for PM2 installation. Let’s use the apt package manager to install them:

$ sudo apt install nodejs npm –y

Next, we will install the pm2 package globally using npm with sudo privileges:

$ sudo npm i -g pm2

To verify the installation, we can use the pm2 -v, which prints the PM2 version available on the system:

$ pm2 -v
5.3.0

In this way, we have successfully installed PM2 on the system.

4. Starting an Application With PM2

Let’s say we have a Node.js web application script named webapp_server.js that we would like to run automatically after each system reboot:

$ cat webapp_server.js
// Node.js program to demonstrate the HTTP server.listen() method

// Import http module
var http = require('http');

// Choose the PORT
const PORT = process.env.PORT || 3000;
...
... output truncated ...
...
// Listen on PORT
httpServer.listen(PORT, () => {
    console.log("Server is running at port 3000...");
});

We’ll invoke the pm2 start command to execute our script through the PM2 process manager:

$ pm2 start webapp_server.js
[PM2] Starting /home/labenv/baels/webapp_server.js in fork_mode (1 instance)
[PM2] Done.
┌────┬──────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬───────────┬─────┐
│ id │ name             │ namespace   │ version │ mode    │ pid      │ uptime │ status    │ ... │
├────┼──────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼───────────┼─────┤
│ 0  │ webapp_server    │ default     │ N/A     │ fork    │ 2544     │ 10m    │ online    │ ... │
└────┴──────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴───────────┴─────┘

The output confirms the process’s start in fork execution mode with one instance. The message “[PM2] Done.” indicates the successful start of the script.

Next, we can obtain an organized view of the processes managed by PM2 using the pm2 list command:

$ pm2 list
┌────┬──────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬───────────┬─────┐
│ id │ name             │ namespace   │ version │ mode    │ pid      │ uptime │ status    │ ... │
├────┼──────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼───────────┼─────┤
│ 0  │ webapp_server    │ default     │ N/A     │ fork    │ 2544     │ 10m    │ online    │ ... │
└────┴──────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴───────────┴─────┘

The processes are labeled by the PM2 process ID, app name, and version, along with the execution mode. Details include system PID, uptime, status, CPU, memory, user, and auto-restart status, providing a concise overview of our managed processes.

4.1. Saving the PM2 Processes

Additionally, we can use the pm2 save command to store the present list of processes managed by PM2 on the local disk. Saving the processes is crucial to ensure that the same applications are automatically restarted when the system reboots. Let’s see how:

$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /home/labenv/.pm2/dump.pm2

Upon execution, we can see that the process has been saved. The outcome is successfully preserving the managed process list in the “/home/labenv/.pm2/dump.pm2” file.

5. Enabling Automatic Startup After Reboot

To create a startup script for PM2, we will use the pm2 startup command, which identifies the init system as systemd and generates a setup command:

$ pm2 startup
[PM2] Init System found: systemd	 
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u labenv --hp/home/labenv

Here, we configure PM2 for automatic startup using systemd by copying and pasting the setup command. Consequently, it adds the PM2 executable path to the system’s PATH variable and sets up the systemd service:

$ sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u labenv --hp /home/labenv
[PM2] Init System found: systemd
Platform systemd
Template
...
... output truncated ...
...
[PM2] [v] Command successfully executed.

Note that this is a one-time configuration of the system. The output confirms the detection of the systemd init system and the successful registration of the PM2 systemd service.

For demo purposes, we’ve rebooted the server once, and the server uptime is extracted using the uptime command. However, the application status and uptime are extracted from the pm2 list command. As highlighted in the output, the application is automatically started by PM2 after the reboot.

$ uptime
00:59:42 up 0 min,  1 user,  load average: 0.47, 0.14, 0.05

$ pm2 list
┌────┬──────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬───────────┬─────┐
│ id │ name             │ namespace   │ version │ mode    │ pid      │ uptime │ status    │ ... │
├────┼──────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼───────────┼─────┤
│ 0  │ webapp_server    │ default     │ N/A     │ fork    │ 1108     │ 34s    │ online    │ ... │
└────┴──────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴───────────┴─────┘

We can see that the uptime reported by the pm2 list command for our webapp_server app name is close to the uptime of the host system since reboot.

Alternatively, we can also test the application state by using the telnet command to connect to the Node.js web application:

$ telnet localhost 3000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

After a successful automatic restart of the application using PM2 following the reboot, we have now confirmed that the application is operational.

6. Conclusion

In this article, we explored using PM2 as a powerful process manager. We saw its installation process and key commands, including script execution, process storage, and organized viewing. Then, we learned how to enable PM2 automatic system startup. Lastly, we showed how to test the automatic startup behavior post-setup to ensure that we have a solution that we can rely on.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.