LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-24-2011, 05:19 AM   #1
sanjay87
Member
 
Registered: Oct 2011
Posts: 164

Rep: Reputation: Disabled
how to get alert when apache fail to start by shell scripts


#!/bin/bash

File_date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`

/etc/init.d/httpd stop

/etc/init.d/tomcat stop

/etc/init.d/mysqld stop

date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`

tar -cvzf /logsbkup/tomcatlog$date.tar /opt/apps/tomcat/apache-tomcat-6.0.26/logs/*

tar -cvzf /logsbkup/apachelog$date.tar /etc/httpd/logs/*

tar -cvzf /logsbkup/mysqllog$date.tar /var/log/*

rm -rf /opt/apps/tomcat/apache-tomcat-6.0.26/logs/*

rm -rf /etc/httpd/logs/*

rm -rf /var/log/*

/etc/init.d/tomcat start

echo "$(date) tomcat service started sucessfully ">>/logsbkup/service-startedstatus-$File_date.log

/etc/init.d/mysqld start

echo "$(date) mysql service started sucessfully ">>/logsbkup/service-startedstatus-$File_date.log

/etc/init.d/httpd start

echo "$(date) apache service started sucessfully ">>/logsbkup/service-startedstatus-$File_date.log

chkconfig --level 345 httpd on

chkconfig --level 345 tomcat on

chkconfig --level 345 mysqld on

echo "$(date) Log tar backup completed sucessfully its moved to /logsbkup directory">>/etc/httpd/logs/daily-backup-$File_date.log

su - logbackup <<EOF

mail -s '$(date) Logs Backup job and services started for $(hostname) - Successful' xx.gmail.com <<EOF

$(date)

HOST=$(hostname)
 
Old 11-24-2011, 07:00 AM   #2
flirek
LQ Newbie
 
Registered: Sep 2011
Posts: 9

Rep: Reputation: Disabled
use netstat -lnp to see if port 80 is open and/or maybe add another check with wget for some static content if can be reached.
in short something like:

set state=`netstat -ln | grep ":80 "|wc -l` #and check variable

and/or

run wget "<some local url>" or some light url and observe return code from wget
 
Old 11-24-2011, 07:08 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Especially with Tomcat startup monitoring tends to be less than reliable. In my experience, it starts up okay, but if there is a problem, it exits after a few seconds or minutes -- depending on the exact cause. (In my case, the culprit is usually not really Tomcat, but the 'ware running in it.)

Consider adding a monitoring script to crontab, so that it is run regularly. Keep a status file in e.g. /var/run/monitor.status, so that you only send mail when the services' status changes. Use the sendmail command to send a digest message (a summary of all services) whenever service status changes. Use the fact that /etc/init.d/service status will return exit status 0 (success) if it is running, and nonzero (error) otherwise.

Here is an untested example script. You need to run it as root; probably the best place to add it is in system crontab.
Code:
#!/bin/bash
servicelist=/etc/monitor.services
statusfile=/var/run/monitor.services
recipient='your.email@example.com'

[ -f "$servicelist" ] || exit 0

# Temporary file for the status report. Autodeleted.
newstatus=$(mktemp)
trap "rm -f '$newstatus'" EXIT

# Generate new service status table
while read service title ; do
    [ -n "$service" ] || continue
    [ -n "$title"   ] || continue
    [ -x "/etc/init.d/$service" ] || continue

    if "/etc/init.d/$service" status &>/dev/null ; then
        printf '%60s: OK\n' "$title"
    else
        printf '%60s: Not running\n' "$title"
    fi
done <"$servicelist" >"$statusfile"

# Status changes?
if cmp -s "$newstatus" "$statusfile" ; then
    # No. Update service list timestamp, then exit.
    touch "$statusfile"
    rm -f "$newstatus"
    exit 0
fi

# Status has changed. Send mail.
mv -f "$newstatus" "$statusfile"
( echo "\"$(hostname -s) service monitor\" <root@$(hostname -f)>"
  echo "To: $recipient"
  echo "Subject: $(hostname -s) service report"
  echo ""
  date
  echo ""
  cat "$statusfile"
  echo "."
) | sendmail -bm -t
The service list file /etc/monitor.services contains service names, one per line, followed by the respective title in the report. Services not installed are ignored. For example:
Code:
ssh          SSH server
apache2      HTTP server (apache2)
tomcat       HTTP server (tomcat)
httpd        HTTP server (httpd)
cups         CUPS print server
It is just an example of such monitoring script, you probably have to edit it to suit your needs (and perhaps even to get it working, since I did not test it). The idea of this script is that it will only send an e-mail if the services' status has changed from the last run. Since it is very lightweight, you could run it every couple of minutes or so, without ill effects.

You can see last status summary always in the status file, /var/run/monitor.services, and the time of last check from the timestamp of that file.

If you have Apache running on the same machine, you could consider running
Code:
#!/bin/sh
echo "Content-type: text/plain"
echo ""
stat -c %y /var/run/monitor.services
echo ""
cat /var/run/monitor.services
as a CGI script; it will always show the latest status report (and its time) in plain text format. With a bit more script, you could have it emitted as a nice HTML table, too, say automatically reloading (using meta refresh) every half update interval?

I hope you find this useful,
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding start-up shell scripts pjdlmack Linux - Newbie 3 10-16-2009 12:23 AM
shell scripts (cgi) via apache ends in 500 snucky Linux - Server 2 06-12-2009 09:48 AM
Running shell scripts on Apache server anirbande Linux - General 2 12-01-2006 08:44 AM
Running shell scripts on Apache server anirbande Linux - General 2 12-01-2006 08:43 AM
fail to start apache with ssl kaon Linux - Software 2 07-07-2005 12:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration