There are often spontaneous service crashes. The user has to deal with their manual recovery. There is no problem as such, if it happens on a home computer. Even more - it is good, because there is a real opportunity to determine the state of the service, identify faults and eliminate them. But the situation is quite different when it comes to servers and VPS services, which must work constantly to provide access to a website or application. In this article we will look at how to configure automatic restarting of Linux services in several ways: with a monitoring script run periodically via cron and in systemd.
The default setting is that Systemd will not do anything with the service if it crashes. But the user can always customize the settings so that in case of a service crash or stop the service will be automatically restarted. For this purpose, the Restart directive is used, which should be added to the Service section. Next, let's look at an example of configuring the automatic restart of the Apache service:
$ sudo systemctl edit apache2
[Service]
Restart=on-failure
RestartSec=5s
Here RestartSec specifies how long to wait before restarting the service. When you are done, save the changes and run the daemon-reload command to reread the configuration:
$ sudo systemctl daemon-reload
Next, to check if everything is working properly, look at the state of the process, terminate the process with a kill signal:
$ sudo systemctl status apache2
$ kill -KILL 32091
Check the status again, the process should be running. To set the initialization to run every time, use a special directive Restart: always. But you should be very careful when using it, because it will not allow you to terminate the process even if it is necessary. If the process is constantly crashing, to make it restart, you can add a limit on the number of restarts to the Service section:
$ sudo systemctl edit apache2
[Service]
StartLimitIntervalSec=500
StartLimitBurst=5
Restart=on-failure
RestartSec=5s
Here StartLimitBurst
and StartLimitIntervalSec
indicate the importance of restarting the service five times, and if it crashes all those five times, to leave it alone and not touch it. The second directive limits service restarts to 500 seconds.
This is probably the most reliable and fail-safe method that works in all versions of Linux. In Apache it is easy to build an automatic restart using a script. To do this, you need to enter the command:
$ sudo vi /usr/local/bin/apache-monitor.sh
#!/bin/bash
ps -A | grep apache2 || systemctl start apache2
Save the file and be sure to make it executable:
chmod ugo+x /usr/local/bin/apache-monitor.sh
Don't forget to add a cron entry to run the script periodically:
$ sudo crontab -e
*/5 * * * * /usr/local/bin/apache-monitor.sh
That's it. Yes, setting up an automatic restart of the service is not as easy as it may seem at first glance. But it is an important ability, so you should pay attention to it - it is definitely worth it.
Apply the discount by inserting the promo code in the special field at checkout:
Apply the discount by inserting the promo code in the special field at checkout: