Bash Script To Restart Linux Server Services Rumi, December 21, 2021 Here’s the command line code to create the file in nano: sudo nano /opt/launch-crashed-services.sh Here is the bash script. #!/bin/bash service mysql status | grep 'active (running)' > /dev/null 2>&1 if [ $? != 0 ] then sudo service mysql restart > /dev/null fi service nginx status | grep 'active (running)' > /dev/null 2>&1 if [ $? != 0 ] then sudo service nginx restart > /dev/null fi service php7.2-fpm status | grep 'active (running)' > /dev/null 2>&1 if [ $? != 0 ] then sudo service php7.2-fpm restart > /dev/null fi Or you can use systemctl instead #!/bin/bash systemctl status mysql | grep 'active (running)' > /dev/null 2>&1 if [ $? != 0 ] then sudo systemctl restart mysql> /dev/null fi systemctl status nginx | grep 'active (running)' > /dev/null 2>&1 if [ $? != 0 ] then sudo systemctl restart nginx > /dev/null fi systemctl status php7.2-fpm| grep 'active (running)' > /dev/null 2>&1 if [ $? != 0 ] then sudo systemctl restart php7.2-fpm > /dev/null fi Change the service names to the ones you are running, e.g. “apache2” or whatever PHP version you are running. The script uses the service <name> status command to output the status of a particular service such as mysql. We then run this through grep looking for the phrase “active (running)”. If this is not found, we ask the system to restart the service. Save the file to /opt/launch-crashed-services.sh Then ensure that it is runnable from the command line using: sudo chmod +x /opt/launch-crashed-services.sh Scheduling Service Restarts Using Crontab It would be a pain to have to SSH into our server every time a service crashes to run the script. Instead we can call the script directly from a crontab service and have it running as frequently as we need it to. Edit your root crontab list using: sudo crontab -e It’s important to use the root crontab using the command above and not to edit your own user profile crontab, otherwise, it will not work properly. Add the following line to the bottom of the root crontab list: */1 * * * * /opt/launch-crashed-services.sh > /dev/null 2> This will run the script every minute but you can change that for whatever works for your server. Now if a critical service crashes, the server will attempt to restart it. Related Administrations Scripts Service RestartSystemctl Status
URL Redirect PHP script for Horde October 12, 2008November 6, 2008 I nearly got mad implementing horde in virtualmin. Though this section will not tell how the Horde was setup. Anyways, HORDE needs to work under /horde framework. I know this can be fixed up in registry.php file and can be used as /groupware or something. Related Read More
Using multiple gateways on Windows system July 3, 2021 If Windows has multiple interfaces connected to several networks that use DHCP, it installs default gateways for all those interfaces. By default, Windows sets the metric of the default gateways to the same, effectively leading to random selection of default gateway. If you want to make Windows to select certain… Read More
Enable Directory Listing in Nginx Webserver October 1, 2011 Here is a simple way to enable directory listing in Nginx Webserver. I have a subdomain repository.wowtutorial.org and i want repository.wowtutorial.org to have ability to listing a directory. All we need to do just modify the vhosts or nginx.conf Add autoindex on; Please see the example below Example : #nano… Read More