How to Automate Process Management on Linux with PM2
To keep PM2-managed apps running and automatically start them when the server is rebooted, you can use the PM2 startup command based on your operating system. Here are the general steps:
- Install PM2 globally if you haven't already:
npm install pm2 -g
- Start your application with PM2:
pm2 start <your-app>
- Use PM2's built-in startup command to generate a startup script for your operating system:
- For Linux:
pm2 startup systemd -u <USER> --hp /home/<USER>
- For macOS:
pm2 startup launchd
- For Windows:
pm2 startup windows
- For Linux:
- Copy and paste the output of the PM2 startup command into your terminal to complete the setup process.
This article focuses specifically on setting up automatic process management on Linux using PM2 with systemd. The pm2 startup systemd -u <USER> --hp /home/<USER>
command sets up a systemd service for PM2 that runs automatically when the system boots up. The -u
parameter specifies the user account that the service will run as, and the --hp
parameter specifies the home directory of that user.
Note that the automatic process management feature described in this article requires PM2 version 2.5.0 or later.