Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

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

Jun 11 2021
designlinux 0 Comments

PM2 is a powerful, widely-used, and feature-rich, production-ready process manager for Node.js. Restarting PM2 with the processes it manages every time your server boots/reboots is critical. One of PM2’s key features is support for a startup script (generated dynamically based on the default init system on your server), that restarts PM2 and your processes at every server restart.

[ You might also like: How to Install PM2 to Run Node.js Apps on Production Server ]

The start script sets up PM2 as a service under the init system. When the server restarts, it will automatically restart PM2, which will then restart all the Node.js applications/processes it is managing.

In this article, we will show you how to deploy PM2 as a service to reliably manage your Node.js apps. For this guide, the test system uses a systemd service and system manager. All commands in this article will be executed as root (use sudo where necessary for a user with privileges to invoke it).

Generate PM2 Start Script for Init System

PM2 is designed to work with the default init system on a Linux system (which it can auto-detect) to generate the startup script and configure PM2 as a service that can be restarted at system boot.

To generate the startup script, simply run the following command as root:

# pm2 startup

The startup sub-command tells PM2 to detect available init system, generate configuration and enable the startup system.

Create PM2 Start Script for Init
Create PM2 Start Script for Init

You can also explicitly specify the init system like so:

# pm2 startup systems

To confirm that the PM2 startup service is up and running under systemd, run the following command (replace the pm2-root.service with the actual name of your service, check the output of the previous command):

# systemctl status pm2-root.service

Start Node.js Applications/Processes

Next, you want to start your Node.js applications using PM2 as follows. If you already have them up and running, started via PM2, you can skip this step:

# cd /var/www/backend/api-v1-staging/
# pm2 start src/bin/www.js -n api-service-staging
Start Nodejs Application
Start Nodejs Application

Next, you need to register/save the current list of processes you want to manage using PM2 so that they will re-spawn at system boot (every time it is expected or an unexpected server restart), by running the following command:

# pm2 save
Save List of Processes
Save List of Processes

Verify PM2 Auto Starting Node.js Apps at Boot

Finally, you need to test if the setup is working fine. Restart your system, and check if all your Node.js processes are running under PM2.

# pm2 ls
or
# pm2 status

Note that you can manually resurrect processes by running the following command:

# pm2 resurrect

Disable the Startup System

You can disable the startup system by running the unstartup sub-command as shown.

# pm2 unstartup
OR
# pm2 startup systemd

To update the startup script, first, disable it, then start it again as shown.

# pm2 unstartup
# pm2 startup

Reference: PM2 Startup Script Generator.

Related

Tags: nodejs, PM2

Rocky Linux 8.3 RC1 Released – Download DVD ISO Images

Prev Post

How to Install Latest OpenOffice in Linux Desktop

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 611
  • 1,055,383

DesignLinux.com © All rights reserved

Go to mobile version