Ensuring Node.js Uptime with Automatic Restart on Server Reboot
Introduction
In today’s digital environment, ensuring uptime for Node.js applications is critical. Even short-term downtimes result in lost revenue, user dissatisfaction, and increased support costs for many applications.
One common challenge DevOps practitioners face is dealing with server restarts. Server restarts due to system updates, hardware issues, or planned maintenance. And if this disruption is not managed properly, it can disrupt the availability of applications. So, we will explain how to start a Node.js backend application using systemd via PM2 automatically.
Using PM2 with Systemd for Automatic Starts
When a server restarts, it terminates all processes, including Node.js applications. These processes then require manual intervention to restart. Automation is essential to quickly and reliably bring applications back online. Thus, it reduces downtime and maintains uninterrupted service.
PM2 is a powerful process manager designed specifically for Node.js applications. Also, it offers essential features such as process management, automatic restart, and real-time monitoring. It ensures that applications run smoothly and remain available even in the face of failures.
Systemd is a comprehensive system and service manager used on Linux servers. It manages all processes from system startup to shutdown. Also, it optimizes operations such as starting, stopping, restarting, and monitoring services.
Systemd enables PM2 to automatically start and manage Node.js processes at system startup. Thus, the backend is restarted after the server is restarted. This combination provides a robust solution for maintaining application uptime.
Prerequisites: Installing PM2 and Systemd for Automatic Start
Before diving into automatic restarts with PM2 and Systemd, let’s first review the installation process on a Linux system:
1. Install Node.js and npm:
Make sure that you install Node.js and npm on your system to use PM2. For Debian/Ubuntu-based systems, run:
# sudo apt update
# sudo apt install nodejs npm
2. Install PM2 Globally:
Install PM2 globally using npm with the following command:
# sudo npm install pm2 –g
After installing PM2, you can proceed to configure PM2 for automatic application restarts on server boot.
Configuring PM2 for Automatic Restarts on Reboot
When a machine restarts (expected or unexpected) Pm2 can create startup scripts and configure them to preserve your process list.
→ sistemd : Ubuntu >= 16, CentOS >= 7, Arch, Debian >= 7
PM2 on command automatically detect startup systems (pm2 startup)
Creating a Startup Script
To automatically create and configure a startup script, simply type the following command:
# pm2 startup
When we run this in the terminal, we will get a direct command.
You must run this command as root. The command result usually looks like this.
# sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu –hp /home/ubuntu
You can make customizations to this command. However, the command returned to us from the terminal is already ready in all its forms. Then copy/paste the displayed command to the terminal and run the command.
PM2 will now automatically restart on startup.
After starting all desired applications, save the application list so that it will occur again after the restart.
# pm2 save
You can reboot your server to test this operation.
# sudo reboot
After a little wait, the server will run and then you can check if pm2 is running automatically. Of course, depending on the size of the server here, the operation of the system files may take a little wait. So, observe for 1-2 minutes.
According to the running operating system (here ubuntu was used) pm2-ubuntu.service file was added as system file.
# sudo service pm2-ubuntu satatus
Disabling the startup system
To disable and remove the current startup configuration:
# pm2 unstartup
Updating the startup script after a node.js version upgrade
When you upgrade the node.js version on your server, you will also need to update the PM2 startup script.
First, disable and remove the existing startup configuration. Therefore, run the following command.
# pm2 unstartup
Then restore a new startup script:
# pm2 startup
By following the same steps above, ensure that pm2 runs automatically as a result of any restart of your server.
Conclusion
When a server restarts, it terminates all processes, including Node.js applications. Without an automated mechanism, these applications do not restart automatically. Also, they require manual intervention to get back online.
Manual restarts can introduce delays and potential errors. And, this leads to service interruptions. Automation is crucial to ensure that applications are quickly and reliably brought back online. Also, it minimizes downtime and maintains seamless user access.
Using PM2 and Systemd to automate Node.js application restarts is crucial for maintaining high availability and reducing downtime. Integrating PM2 with Systemd allows automatic application restarts after server reboots. Thus it removes the need for manual intervention and lowers the risk of service interruptions. Implementing this setup in your production environment not only enhances reliability but also simplifies server management. For more details on configuring PM2 with Systemd, visit the [PM2 Documentation](https://pm2.keymetrics.io/docs/usage/quick-start/) and explore additional [related resources](https://pm2.keymetrics.io/docs/usage/pm2-docs/).
Mysoly | Your partner in digital!
bilal cangal serkan kilic