Check if Apache2 is running and send email to my gmail if it's not running

Hi Experts,

Can you guys help me with a script to determine if the Apache2 is running.  If it's not running it will send and email to my gmail account.

Is this possible using shell script?

Any help is greatly appreciated.
m3mdiclAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel HelgenbergerCommented:
This should do the trick, uses mailx to mail and most importantly the nagios check_http plugin to do the testing. Get it or use your package system to install the plugin. Make sure to configure the variables accordingly and run it with cron. To test it, run it once against a non existent host.
The script mails you if check_http finds errors and warnings (eg. high latencies).
The plugin runs basically a status check - so it detects Status 404 and 500 as well as a downed httpd.

The script can be run on any host which is able to reach $HOST, btw.

#!/bin/bash
# uses nagios check_http and email on error
# put this in /etc/cron.hourly or something

CHKCMD=/usr/lib64/nagios/plugins/check_http
HOST=www.google.com
MAILTO=root@localhost
MAILX=/bin/mailx

#do the check, test for exit code
STATUS=$($CHKCMD -H $HOST)
EXITCODE=$?

if ! [ $EXITCODE -eq 0 ]; then
	echo $STATUS|$MAILX -s "Webserver down - $HOST" $MAILTO
fi

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
m3mdiclAuthor Commented:
Need something using shell scripting. No nagios plugins.
0
Daniel HelgenbergerCommented:
Why, what do you mean with 'shell scripting' in particular? check_http is perfect for this, as I pointed out it checks on http status codes as well.

Another question: What are you able to use? curl at leat?
0
Daniel HelgenbergerCommented:
Ok, modified it with curl. Much more crude, but does the same. Checks if it finds a "200 OK" status code. If not, the sting is empty (=error I hope) and an emal will be send.
#!/bin/bash
# uses curl to check for "200 OK" and email on error

HOST=http://www.google.com/index.html
MAILTO=root@localhost
MAILX=/bin/mailx
CURL=/usr/bin/curl

#do the check, test for exit code
$STATUS=$($CURL -s -I $HOST|grep "200 OK")

if [ -z $STATUS ]; then
	$CURL -s -I $HOST|$MAILX -s "Website down - $HOST" $MAILTO
fi

Open in new window

0
skullnobrainsCommented:
echo -e "GET / HTTP/1.1\nUser-Agent: shell/tester\nAccept: */*\nHost: localhost\n\n" | nc localhost 80 | grep 'This is the default web page' || mail -s "apache is dead" YOURACCOUNT@gmail.com

Open in new window


this assumes that the mail command will work (ie your local MUA is properly configured and running)

you can change the HOST header to match vhosts, the "/" to match a different path, and the grep content to match for example the title of the page you are checking
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.