Hi Guys,
My Apache Web-server was playing up a little bit so until I found the cause of it my priority was to keep it up and running so I wrote a bash script to keep an eye on httpd crashes
Lets check it out
First up is to pick a script name and named it as httpdmon.sh and it is stored in my /home/jas/ directory
CronJob
Next I would just set a cron job so that it runs every minute and restart/start the service if required.
1 |
* * * * * /home/jas/httpmon.sh |
Basic Script
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/bin/bash response=$(curl --write-out %{http_code} --connect-timeout 5 --silent --output /dev/null http://YOUR_SITE.COM/index.html) if [ $response -eq 200 ] then echo "All Working well"; else echo "Restarting Service" (/etc/init.d/httpd restart) fi # Auth - Jas Chahal <span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px;"> </span> |
Above script is good and as you can see that I am using curl to shoot a request to my local website and if the request times out i.e. after 5 seconds then an attempt to restart the service is made
Now thats Ok but I would also like to be notified about it and also would like to force kill httpd process if for some reason it hangs
A little bit more added to above script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/bin/bash SUBJECT="restart" EMAIL="youremailaddress@gmail.com" MESSAGE="/path/to/message.txt" response=$(curl --write-out %{http_code} --connect-timeout 5 --silent --output /dev/null http://YOUR_SITE.com/index.html) if [ $response -eq 200 ] then echo "All Working well"; else echo "Launching" (kill $(ps aux | grep '[h]ttpd' | awk '{print $2}')) (/etc/init.d/httpd restart) /bin/mail -s "$SUBJECT" "$EMAIL" < $MESSAGE fi # Auth - Jas Chahal |
As you can see that above will send me a message, you can replace mail command with sendmail etc its up to you.
Running that solved few of my issues until I figured out what is causing httpd to crash. If I found something interesting I will be sharing it with you guys.
Please note: I am not a Linux Advanced user but just knows enough to do some basic to intermediate things. If you have something to add to would like to improve above script please leave your comments as it would help others.
Cheers,
if we dont have the website in server.
wanted to check the httpd service is running or not, if that is not running need to restart httpd service, dont have ping option too., ping also disabled.