How to Configure Automatic Application Restart After a Server Reboot
How to Configure Automatic Application Restart After a Server Reboot

When a server restarts unexpectedly or undergoes maintenance, applications running on it may stop working. For businesses that depend on websites, APIs, and backend services, this can lead to downtime, failed requests, and interrupted operations.
Configuring automatic application restart helps ensure that applications start again when the server comes back online. This is especially important for Node.js applications, background workers, APIs, and other long-running services.
In this guide, we will explain how to configure automatic application restart after a server reboot using PM2, systemd, and Windows Task Scheduler. We will also cover common problems and best practices for keeping applications available after restarts.
Why Is Automatic Application Restart Important?
Applications started manually from a terminal may stop when the session ends or when the server reboots. Without a startup configuration, someone may need to log in and restart them manually.
Automatic restart configuration helps businesses:
Reduce downtime after planned or unexpected reboots.
Keep websites and APIs available without manual intervention.
Recover application processes after system maintenance.
Improve reliability for production services.
Reduce repetitive server administration tasks.
However, restarting a process does not guarantee that the application is healthy. Database connections, external APIs, and other dependencies must also be available.
1. Configure Automatic Restart for Node.js Using PM2
PM2 is a process manager for Node.js applications. It can restart applications after crashes and restore saved processes after a server reboot when startup integration is configured correctly.
Step 1: Start Your Application With PM2
Navigate to your application directory:
cd /var/www/my-node-appStart the application using its npm start script:
pm2 start npm --name my-node-app -- startCheck the application status:
pm2 statusMake sure the application is running successfully before proceeding.
Step 2: Configure PM2 Startup
Run the following command:
pm2 startupPM2 will display a startup command specific to your system and user account. Copy and execute the command it provides.
This configures the system to start PM2 automatically during boot.
Step 3: Save the Process List
Run:
pm2 saveThis saves the current list of managed applications so PM2 can restore them when its startup integration runs.
If you add or remove applications later, update the saved list as needed.
Step 4: Verify the Configuration
After scheduling a safe reboot during an approved maintenance window, reconnect to the server and run:
pm2 statusThen check the application logs:
pm2 logs my-node-appConfirm that the application is responding to requests, not merely showing a running process status.
2. Configure Automatic Restart Using systemd
For applications that do not use PM2, Linux's systemd service manager can start applications automatically at boot and restart them when they exit unexpectedly.
Create a service file:
sudo nano /etc/systemd/system/myapp.serviceAdd a configuration similar to this:
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
User=appuser
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/node /var/www/myapp/server.js
Restart=on-failure
RestartSec=5
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.targetReplace appuser, the application directory, and the Node.js executable path with the correct values for your server.
The Restart=on-failure setting tells systemd to restart the application when it exits unsuccessfully. Other restart policies are available depending on your requirements.
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now myappCheck the service status:
sudo systemctl status myappTo inspect recent logs:
sudo journalctl -u myapp -n 100 --no-pagerUse either systemd directly or PM2 for process supervision of a given application unless you have a clear reason to combine them. Running multiple supervisors for the same process can complicate troubleshooting.
3. Configure Automatic Restart on Windows Server
Windows Server supports automatic application startup through Task Scheduler and Windows services.
For a simple application that needs to start after a reboot, Task Scheduler can run a script at system startup.
Step 1: Create a Startup Script
For example, create a file named start-app.bat:
@echo off
cd /d C:\apps\myapp
npm startAdjust the directory and start command to match your application.
Step 2: Create a Scheduled Task
Open Task Scheduler.
Select Create Task.
Enter a descriptive task name.
Configure a trigger for system startup.
Under Actions, select Start a program.
Choose your startup script.
Configure the task to run under an appropriate account with the required permissions.
Save the task.
Step 3: Test the Task
Run the task manually and confirm that the application starts correctly. Then test it after a planned server reboot.
For long-running production applications, a proper Windows service manager or a service-based deployment may be more suitable than a basic startup script. Configure logging and recovery behavior so failures can be investigated.
4. Common Problems and How to Fix Them
Even after configuring automatic startup, applications may fail to return after a reboot.
Application is missing from PM2: Check that you ran pm2 save under the correct user account and configured PM2 startup for that same account.
Application starts but immediately stops: Review application logs, environment variables, file permissions, and database connectivity.
The startup command cannot find Node.js: Check the executable path and ensure the startup environment can access the required runtime.
The application runs but the website is unavailable: Verify Nginx or another reverse proxy, firewall rules, application ports, and dependent services.
The server reboots repeatedly: Investigate the underlying server issue. Automatic application restart cannot fix an operating-system crash, insufficient memory, or a failing dependency.
5. Best Practices for Reliable Application Recovery
To make application recovery more reliable:
Use PM2 or systemd to supervise long-running Linux applications.
Save and verify startup configurations after deployment changes.
Run applications with the least privileges required.
Keep credentials in secure configuration files or a secrets manager.
Configure application logs and monitoring alerts.
Ensure databases and required services are available during startup.
Test recovery during planned maintenance windows.
Maintain backups and a documented recovery procedure.
For business-critical applications, add health checks and external uptime monitoring. These can identify cases where the process is running but the application is not actually serving requests.
Conclusion
Configuring automatic application restart after a server reboot helps reduce manual intervention and improve application availability. PM2 is a practical option for Node.js applications, systemd provides native process management on Linux, and Windows Server offers startup options through Task Scheduler and service management tools.
The right approach depends on your operating system and application architecture. After configuring automatic startup, always verify the process, logs, and actual application response to confirm that recovery works as expected.
Need help improving your server reliability? Dotcube Technologies can help configure application process management, server startup automation, monitoring, and deployment workflows for production web applications.


