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
Softether with DNSMasq on Debian 11 February 26, 2024 What is SoftEther A Free Cross-platform Multi-protocol VPN program, as an academic project from University of Tsukuba. It is a single server which support SSL-VPN (HTTPS) and 6 major VPN protocols (OpenVPN, IPsec, L2TP, MS-SSTP, L2TPv3 and EtherIP). I would like to also highlight the VPN over ICMP and VPN over… Read More
How to increase memory size for MySQL Server June 14, 2020June 14, 2020 To increase the memory size for a MySQL Server, follow these steps: 1.Enter management mode by typing your password and pressing Enter twice. Select Exit to terminal using the arrow keys and then press Enter 2.Type: nano /etc/my.cnf 3.Locate the line innodb_buffer_pool_size = 1024M and change the number to 50%… Read More
Clean up boot partition – Ubuntu 14.04LTS-x64, Ubuntu 16.04LTS-x64 December 27, 2018 Case I: if /boot is not 100% full and apt is working 1. Check the current kernel version $ uname -r It will shows the list like below: 3.19.0-64-generic 2. Remove the OLD kernels 2.a. List the old kernel $ sudo dpkg –list ‘linux-image*’|awk ‘{ if ($1==”ii”) print $2}’|grep… Read More