In a Linux system, services (also called daemons) play a critical role in handling various tasks such as web hosting, database management, and networking. However, services can sometimes crash or stop due to errors, high resource usage, or unexpected system failures.
To prevent downtime and ensure smooth operations, system administrators can configure services to restart automatically whenever they fail, which is especially useful for web servers (Apache, Nginx), databases (MySQL, PostgreSQL), or other critical applications that need to be available at all times.
In this guide, we’ll explain how to use systemd to configure a Linux service to restart automatically if it stops.
Why Restart a Service Automatically?
There are several reasons why you might want to automatically restart a service in Linux:
- Minimize downtime: If a service stops unexpectedly, automatic restarts ensure that users experience minimal disruption.
- Improve reliability: Services like web servers, databases, and background processes should always be running.
- Reduce manual work: Without automation, you’d need to check services frequently and restart them manually if they fail.
- Handle unexpected failures: If a service crashes due to software bugs, resource limits, or system errors, the systemd can restart it without admin intervention.
Now, let’s go through the steps to set up automatic restarts using systemd.
Step 1: Identify the Service You Want to Restart
Before making changes, you need to know the exact name of the service you want to configure by listing all running services.
systemctl list-units --type=service --state=running
If you already know the service name, you can check its status.
systemctl status apache2

Replace apache2
with the actual service name you want to manage.
Step 2: Edit the Service Configuration
Systemd allows you to modify service behavior using custom configuration files. Instead of modifying system-wide settings (which can be overwritten during updates), we’ll use systemctl edit
to create an override file.
Run the following command:
systemctl edit apache2
This will open a blank file in your default text editor.
If the file isn’t empty, you’ll see existing settings that you can modify. Otherwise, you’ll need to add the necessary restart configuration.

Step 3: Add Systemd Restart Configuration
In the editor, add the following lines.
[Service] Restart=always RestartSec=5s
Explanation of these settings:
Restart=always
– Ensures that the service restarts whenever it stops, regardless of the reason.RestartSec=5s
– Tells systemd to wait 5 seconds before restarting the service, which can prevent rapid restart loops in case of repeated failures.
Once added, save and close the file.

After making changes to a systemd service, you need to reload systemd and restart the service to ensure the new configuration is applied:
sudo systemctl daemon-reload sudo systemctl restart apache2
To confirm that the service is now set to restart automatically, run:
sudo systemctl show apache2 | grep Restart
If everything is configured correctly, you should see:
Restart=always
Step 4: Test the Automatic Restart in Linux
To ensure the configuration works, you can manually stop the service and check if it restarts.
sudo systemctl stop apache2
Wait for 5 seconds, then check its status.
sudo systemctl status apache2
If the service is running again, the automatic restart is working!
Additional Restart Options
Depending on your needs, systemd provides different restart policies:
Restart=always
– The service always restarts, even if it was manually stopped.Restart=on-failure
– Restarts only if the service exits with an error (but not if stopped manually).Restart=on-abnormal
– Restarts the service if it crashes due to a signal (like a segmentation fault).Restart=on-watchdog
– Restart the service if it times out while running.
You can replace Restart=always
with any of these options based on your requirements.
How to Check Service Logs for Issues
If a service keeps failing, it’s a good idea to check logs using the journalctl command, which will show logs for the service from the last 10 minutes.
journalctl -u apache2 --since "10 minutes ago"
For a real-time log stream, use:
journalctl -u apache2 -f
Conclusion
Setting up automatic restarts for failing services ensures that critical applications keep running without manual intervention. By using systemd’s restart options, you can minimize downtime, improve system stability, and reduce the need for manual troubleshooting.