Bash script to restart Apache automatically
Bash script to restart Apache automatically
Sometimes very strange if your apache went down due to crashing, In order to perform auto restart we can set a script on the cronjob.
Before that you need to install nc on your server. ncat or nc is networking utility with functionality similar to cat command but for network. It is a general purpose CLI tool for reading, writing, redirecting data across a network. It is designed to be a reliable back-end tool that can be used with scripts or other programs. It’s also a great tool for network debugging, as it can create any kind of connect one can need.
Step1. Login to SSH
Step2 . Install nc
yum install nc
Step3. vim /root/Apache_checker.sh
if nc -z -w2 Server_IP 80 2>/dev/null; then echo "Connected" else echo "no connection" service httpd start fi
Step4. chmod 755 /root/Apache_checker.sh
Step5. Set cronjob to run every minute
cronatab -e
Add the below task and save
*/1 * * * * /root/Apache_checker.sh >> /var/log/apache_checker_log 2>&1
Thanks