How to restart PM2 automatically after server / system reboot?

PM2 (Process Manager 2) is a popular process manager for Node.js applications that provides a straightforward way to manage, monitor, and keep your applications running. To ensure that your PM2-managed applications restart automatically after a server or system reboot, you can follow these steps:

1. Install PM2 (if not already installed)

If you haven’t installed PM2 yet, you can install it globally using npm (Node Package Manager):

Bash
npm install -g pm2

2. Start Your Application with PM2

Navigate to your application’s directory and start your application using PM2:

Bash
pm2 start app.js --name "my-app"

Replace app.js with the entry point of your application and "my-app" with the desired name for your application process.

3. Set PM2 to Start at Boot

To make sure PM2 itself starts on boot, you need to generate and activate a startup script for your operating system.

For Ubuntu/Debian:
Bash
pm2 startup systemd

Follow the instructions that PM2 provides after running this command. It usually involves running a given systemctl command.

For CentOS/RedHat:
Bash
pm2 startup

Again, follow the instructions that PM2 provides after running this command.

For other systems:

PM2 supports multiple init systems like systemd, upstart, and sysv, so you can adjust the startup script based on your system’s init system.

4. Save Current PM2 Process List

Save the list of PM2 processes to ensure they are restarted on boot:

Bash
pm2 save

This command will save the current list of PM2 processes so that they can be automatically restarted upon system reboot.

5. Enable PM2 Process Restart on Boot

After running pm2 startup, PM2 will provide a command that you need to run to enable the process restart on boot. It usually looks something like this:

Bash
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u username --hp /home/username

or

Bash
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u username --hp /home/username

Replace username with your actual username.

6. Verify PM2 Startup Configuration

To verify that PM2 will restart your applications on boot, you can reboot your server or system and then check the status of your PM2 processes:

Bash
pm2 status

This command will show you the status of all PM2-managed processes. If everything is configured correctly, you should see your application processes running.

7. Optional: Configure PM2 Process Settings

You can also configure additional settings for your PM2-managed processes using the PM2 ecosystem file. Create a file named ecosystem.config.js in your application directory and define your application configurations:

JavaScript
module.exports = {
  apps: [
    {
      name: "my-app",
      script: "./app.js",
      watch: true,
      env: {
        NODE_ENV: "production",
      },
    },
  ],
};

After creating the ecosystem file, you can start your application using:

Bash
pm2 start ecosystem.config.js

By following these steps, you can ensure that your PM2-managed applications will restart automatically after a server or system reboot, providing high availability and reliability for your Node.js applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart