Portal Home > Knowledgebase > Articles Database > Script to Monitor and Auto Restart Apache Httpd Server


Script to Monitor and Auto Restart Apache Httpd Server




Posted by 3-rx, 01-22-2009, 04:26 PM
hi, As I am new to Linux server, I am facing some doubts like the Script to can monito my unmanaged server and Auto Restart Apache Httpd Server I'm on linux server, OS- Centos, control panel- DA. I setup only 5-6 my websites on this server, which don't have so much traffic; installed few php scripts, which eat a lot of memory. I setup 2gb RAm, but unfortunately the server last 2 weeks often went to offline, as server was out of memory. after a little research find few scripts 1. Linux Server Hack - How to setup a Shell Script to Auto Restart Apache Httpd Server! Save the following as restart.sh: #!/bin/bash # Apache Process Monitor # Restart Apache Web Server When It Goes Down # ------------------------------------------------------------------------- # Copyright (c) 2003 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script http://en.wikipedia.org/wiki/Shell_script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- # RHEL / CentOS / Fedora http://en.wikipedia.org/wiki/Fedora Linux restart command RESTART="/sbin/service httpd restart" # uncomment if you are using Debian / Ubuntu Linux #RESTART="/etc/init.d/apache2 restart" #path to pgrep command PGREP="/usr/bin/pgrep" # Httpd daemon name, # Under RHEL/CentOS/Fedora it is httpd # Under Debian 4.x it is apache2 HTTPD="httpd" # find httpd pid $PGREP ${HTTPD} if [ $? -ne 0 ] # if apache not running then # restart apache $RESTART fi Then setup a cron job like this: (usually in your sa-update file under /etc/cron.d) */5 * * * * root /root/restart.sh >/dev/null 2>&1 source: (http://zedomax.com/blog/2008/07/28/linux-server-hack-how-to-setup-a-shell-script-to-auto-restart-apache-httpd-server/) 2. script to monitor your server --- BEGIN --- #!/home/www/cgi-bin/php -q "") { $log .= "Executing ".$action[$n]." ...\n"; exec($action[$n]); $mailheaders = "From: ".$from_email."\nContent-Type: tex t/plain; charset=\"us-ascii\""; mail ($admin_email, "Restarting ".$processes[$n], $email _body, $mailheaders); } else { $log .= $action[$n]." not running!\n"; $mailheaders = "From: ".$from_email."\nContent-Type: tex t/plain; charset=\"us-ascii\""; mail ($admin_email, $processes[$n]." not running!", $ema il_body, $mailheaders); } } else if ($process_count[$n] > $max_limit[$n]) { $log .= "Too many (".$process_count[$n].") ".$processes[$n]." pr ocesses. Sending notice to $admin_email\n"; $mailheaders = "From: ".$from_email."\nContent-Type: text/plain; charset=\"us-ascii\""; mail ($admin_email, "Too many (".$process_count[$n].") ".$proces ses[$n]." processes", $email_body, $mailheaders); } } // Uncomment following line to print log to stdout #echo $log; ?> --- END --- source (http://forums.olm.net/showthread.php?t=415) 3. Shell script to restart MySQL server if it is killed or not working #!/bin/bash # Shell script to restart MySQL server if it is killed or not working # due to ANY causes. # When script detects mysql is not running (it basically sends ping request # to MySQL) it try to start using /etc/init.d/mysql script; and it sends an # email to user indicating the status. # This script must be run from Cron Job so that it can monitor mysql server. # For more info visit following url: # http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/08/linux-mysql-server-monitoring.html # -------------------------------------------------------------------------- # Copyright (C) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- # mysql root/admin username MUSER="root" # mysql admin/root password MPASS="SET-ROOT-PASSWORD" # mysql server hostname MHOST="localhost" #Shell script to start MySQL server i.e. path to MySQL daemon start/stop script. # Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS. MSTART="/etc/init.d/mysql start" # Email ID to send notification EMAILID="notification@somewhere-corp.com" # path to mail program MAILCMD="$(which mail)" # path mysqladmin MADMIN="$(which mysqladmin)" #### DO NOT CHANGE anything BELOW #### MAILMESSAGE="/tmp/mysql.fail.$$" # see if MySQL server is alive or not # 2&1 could be better but i would like to keep it simple and easy to # understand stuff $MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null if [ $? -ne 0 ]; then echo "" >$MAILMESSAGE echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE echo "Hostname: $(hostname)" >>$MAILMESSAGE echo "Date & Time: $(date)" >>$MAILMESSAGE # try to start mysql $MSTART>/dev/null # see if it is started or not o=$(ps cax | grep -c ' mysqld$') if [ $o -eq 1 ]; then sMess="MySQL Server MySQL server successfully restarted" else sMess="MySQL server FAILED to restart" fi # Email status too echo "Current Status: $sMess" >>$MAILMESSAGE echo "" >>$MAILMESSAGE echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE # send email $MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE else # MySQL is running and do nothing : fi # remove file rm -f $MAILMESSAGE source (http://bash.cyberciti.biz/monitoring/monitor-mysql-server-shell-script/) 4. Monit M/Monit is a system for automatic management and monitoring of Information Technology Systems. M/Monit can monitor and manage distributed computer systems, conduct automatic maintenance and repair and execute meaningful causal actions in error situations. M/Monit can start a service if it does not run, restart a service if it does not respond and suspend a service if it utilize too much resources. Monitor system attributes such as CPU, Load, Memory, Disk usage, files, directories and filesystems for changes. Conditional rules can be set and if a value goes outside a defined scope specific actions can be executed and notification sent. Information is collected from monitored systems and stored in a database. Drill-down and filter functions exist to investigate collected data. source (http://mmonit.com) =----------- So need your advice which one you recommend. Any advice will be appreciated



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
php 4/5 and apache 2? (Views: 240)