How to Enable PM2 to Auto Start Node.js App at System Boot?

PM2 is a process manager for Node.js applications that provides automatic restart on crashes, multi-application management, and detailed logging. It simplifies Node.js application management by automating common tasks like starting, stopping, and restarting processes while offering real-time performance monitoring capabilities.

Enabling auto-start at system boot is critical for production Node.js applications. When servers restart for maintenance or updates, applications must automatically resume without manual intervention to ensure continuous service availability.

Installing PM2

Before installing PM2, ensure Node.js is installed by running node -v in your terminal. If not installed, download it from the official Node.js website.

Install PM2 globally using npm

npm install pm2 -g

Setting Up Your Node.js Application

Navigate to your application directory and start it with PM2

cd /path/to/your/nodejs/app
pm2 start app.js

Verify the application is running

pm2 list

This displays all applications currently managed by PM2.

Configuration with Ecosystem File

Create an ecosystem.config.js file to configure your application with environment variables and other settings

module.exports = {
  apps: [{
    name: "myapp",
    script: "app.js",
    env: {
      NODE_ENV: "development",
      PORT: 3000
    },
    env_production: {
      NODE_ENV: "production",
      PORT: 8080
    }
  }]
}

Start the application using the ecosystem file

pm2 start ecosystem.config.js --env production

Enabling Auto Start at System Boot

PM2 provides a built-in command to generate and install startup scripts for your operating system. This ensures your applications automatically start when the system boots.

Using PM2 Startup Command

Generate the startup script for your system

pm2 startup

This command detects your operating system and provides the appropriate command to run. Execute the displayed command with sudo privileges.

Save your current PM2 process list

pm2 save

This creates a dump file that PM2 will use to resurrect your applications on system boot.

Manual systemd Configuration (Linux)

For more control on Linux systems, create a custom systemd service

sudo nano /etc/systemd/system/myapp.service

Add the following configuration

[Unit]
Description=My Node.js App
After=network.target

[Service]
Type=forking
User=nodeuser
WorkingDirectory=/path/to/your/app
Environment=NODE_ENV=production
ExecStart=/usr/lib/node_modules/pm2/bin/pm2 start ecosystem.config.js --no-daemon
Restart=always

[Install]
WantedBy=multi-user.target

Enable and start the service

sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service

Advanced Configuration Options

Custom Startup Scripts

PM2 supports pre and post execution hooks in your ecosystem file

module.exports = {
  apps: [{
    name: "my-app",
    script: "app.js",
    pre_start: "/path/to/pre-start-script.sh",
    post_start: "/path/to/post-start-script.sh",
    env_production: {
      NODE_ENV: "production",
      PORT: 8080
    }
  }]
}

Process Monitoring and Management

Monitor your applications with PM2's built-in commands

pm2 status          # View process status
pm2 logs            # View logs
pm2 monit          # Real-time monitoring
pm2 restart all    # Restart all processes
pm2 stop all       # Stop all processes

Cluster Mode

Utilize multiple CPU cores with cluster mode

module.exports = {
  apps: [{
    name: "my-app",
    script: "app.js",
    instances: "max",  // Use all available CPUs
    exec_mode: "cluster"
  }]
}

Troubleshooting

Issue Solution
App not starting on boot Check pm2 startup was run and pm2 save was executed
Permission errors Ensure proper user permissions and run startup command with sudo
Environment variables missing Verify ecosystem.config.js has correct env settings
Process crashes on startup Check logs with pm2 logs and verify file paths

Conclusion

PM2's auto-start functionality ensures your Node.js applications remain available after system reboots. Using the pm2 startup and pm2 save commands provides a simple solution, while custom systemd services offer greater control. Proper configuration with ecosystem files enables environment-specific settings and advanced features like clustering for production deployments.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements