PM2 Auto Start Node.js App: Setup Guide
In this article
Yes, you can enable PM2 to auto-start your Node.js app on system boot. PM2 provides the pm2 startup command to generate and configure a system service (like systemd on Linux), and pm2 save to persist the current process list. This guide walks you through the exact steps, explains how it works, and covers common issues.
Why Use PM2 for Auto-Start?
When you run a Node.js app with node app.js, it stops when you close the terminal or reboot the server. PM2 is a process manager that keeps your app running in the background, restarts it if it crashes, and can start it automatically when the system boots. This is essential for production servers that need to be available 24/7 without manual intervention.
Prerequisites
- Node.js and npm installed on your server.
- PM2 installed globally:
npm install -g pm2. - Your Node.js app is already running with PM2 (e.g.,
pm2 start app.js). - You have sudo/root access to create system services.
Step-by-Step: Enable Auto-Start
- Start your app with PM2 (if not already):
pm2 start app.js --name my-app. - Generate the startup script: Run
pm2 startup. PM2 will output a command that you need to run with sudo (it varies by OS). For example, on Ubuntu with systemd, it might say:sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u youruser --hp /home/youruser. Copy and run that exact command. - Save the current process list: Run
pm2 save. This stores the list of PM2 processes so that on boot, PM2 knows which apps to start. - Reboot to test: Restart your server and run
pm2 listto see if your app is running automatically.
How PM2 Startup Works
PM2 creates a system service (e.g., a systemd unit file) that runs at boot. The service starts the PM2 daemon, which then reads the saved process list (from pm2 save) and launches each app. The pm2 startup command must be run with the correct user and home directory, because PM2 runs as that user. If you run it as root, the service will run as root, which is not recommended for security.
Common Problems and Fixes
- PM2 not starting after reboot: Check that you ran
pm2 saveafter the last change to your process list. Also verify the startup script is enabled:systemctl status pm2-(on systemd). - Permission errors: Ensure the startup command uses the correct user and home path. If you moved your project, re-run
pm2 startupandpm2 save. - App fails to start on boot because of environment variables: PM2 does not load your shell's environment by default. Use
pm2 envor an ecosystem file to set environment variables explicitly. - Using a different Node.js version: If you use nvm, the startup script may point to the wrong Node path. Specify the full path to the Node binary in your ecosystem file or use
pm2 start app.js --interpreter /path/to/node.
Comparing PM2 with Alternatives
PM2 is popular because it is easy to use and cross-platform. Alternatives include:
- systemd: Native Linux service manager. You can write a unit file manually, but it lacks PM2's clustering, log management, and monitoring features.
- Forever: Older process manager, but it does not have a built-in startup script generator as robust as PM2's.
- Docker: If you containerize your app, you can use Docker's
restart: alwayspolicy. This is great for isolation, but adds complexity.
Best Practices for Production
- Use an ecosystem file (
ecosystem.config.js) to define your app's settings, environment variables, and instances. Then start withpm2 start ecosystem.config.js. - Run PM2 as a non-root user. Create a dedicated user for your app if needed.
- Set up log rotation with
pm2 install pm2-logrotateto avoid filling up your disk. - Monitor your app with
pm2 monitor integrate with a monitoring service. - Regularly run
pm2 saveafter any changes to your process list.
Frequently Asked Questions
Do I need to run pm2 startup every time I add a new app?
No, you only need to run pm2 startup once per system. After adding a new app, just run pm2 save to update the saved process list.
How do I disable auto-start for a specific app?
You can delete the app from PM2 (pm2 delete app-name) and then run pm2 save. Or, if you want to keep it but not start it on boot, you can stop it and save, but it will not be started by PM2 at boot.
What if my app uses a database that isn't ready when PM2 starts?
You can add a delay or retry logic in your app, or use PM2's --wait-ready option with process.send('ready') in your code. Alternatively, use a process manager like systemd with dependencies.
Does PM2 work on Windows for auto-start?
PM2 startup is primarily for Linux and macOS. On Windows, you can use PM2 as a Windows service with pm2-installer or use pm2-startup with a scheduled task, but it is less straightforward. Many Windows users prefer using the built-in Task Scheduler or NSSM.
Can I use PM2 with a cluster of Node.js instances?
Yes, PM2 supports cluster mode. Start with pm2 start app.js -i max to use all CPU cores. The startup script will start all instances on boot.
How do I update PM2 without losing my saved processes?
Run npm install -g pm2@latest and then pm2 update to refresh the daemon. Your saved process list remains intact.