<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>LINICKX.com</title><link>https://www.linickx.com/</link><description></description><lastBuildDate>Tue, 06 Mar 2007 13:32:00 +0000</lastBuildDate><item><title>Service Recovery Scripts &amp; Error Page Tips.</title><link>https://www.linickx.com/service-recovery-scripts-error-page-tips</link><description>&lt;p&gt;A couple of weeks ago, I was proper ill with flu; the problem with
looking after your own server is that only you can fix it - it's well
and good having monitoring systems (&lt;em&gt;nagios&lt;/em&gt;) telling you about faults,
but if you can't read or see the alerts the fault won't get resolved.&lt;/p&gt;
&lt;p&gt;During this time I was ill, for an unknown reason the mySQL process on
my server died, as such my website (&lt;em&gt;and others I look after&lt;/em&gt;) were down
for 8 hours. The fix was simple, one command, restart the service and
normal service was resumed (&lt;em&gt;excuse the pun&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;This led to me to the conclusion that there must be a way to get the
server to fix it's self. after all, why do a job when you can get a
computer to do it for you ! Fortunately I had a
&lt;abbr title="short moment of genuis"&gt;light bulb moment&lt;/abbr&gt; and
realised that I could use the init scripts that are provided by redhat,
the below code will restart apache (&lt;em&gt;httpd&lt;/em&gt;) and mySQL on a redhat based
system in the event that the service was not stopped cleanly. (&lt;em&gt;In-fact
this config has only be tested on CentOS, your mileage may vary on
anything else&lt;/em&gt;)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/bin/bash

# taken from redhast default scripts - /etc/rc.d/init.d/functions

# Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
export PATH

status() {
        local base=${1##*/}
        local pid

        # Test syntax.
        if [ "$#" = 0 ] ; then
                echo $"Usage: status {program}"
                return 1
        fi

        # First try "pidof"
        pid=`pidof -o $$ -o $PPID -o %PPID -x $1 ||
             pidof -o $$ -o $PPID -o %PPID -x ${base}`
        if [ -n "$pid" ]; then
# Uncomment this if you want OK messages
#               echo $"${base} (pid $pid) is running..."
                return 0
        fi

        # Next try "/var/run/*.pid" files
        if [ -f /var/run/${base}.pid ] ; then
                read pid &amp;lt; /var/run/${base}.pid
                if [ -n "$pid" ]; then
                        echo $"${base} dead but pid file exists"
                        /etc/init.d/${base} restart
                        return 1
                fi
        fi
        # See if /var/lock/subsys/${base} exists
        if [ -f /var/lock/subsys/${base} ]; then
                echo $"${base} dead but subsys locked"
                /etc/init.d/${base} restart
                return 2
        fi
        echo $"${base} is stopped"
        return 3
}

# found in /etc/init.d/httpd
httpd=${HTTPD-/usr/sbin/httpd}

status mysqld
status $httpd
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you save this, as &lt;strong&gt;/etc/cron.hourly/auto_recovery.sh&lt;/strong&gt; , then do
&lt;strong&gt;chmod +x /etc/cron.hourly/auto_recovery.sh&lt;/strong&gt; , assuming you've not
changed the default cron setup, every hour mySQL &amp;amp; httpd will be
checked, if they have died the'll be restarted and root will get an
e-mail about what happened.&lt;/p&gt;
&lt;p&gt;Cool eh !&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A final finishing touch:&lt;/strong&gt; I wanted to change the default "Database
Down" error messages on my two most popular applications.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://allforces.com/2006/06/18/custom-wordpress-errors/"&gt;Melvin Rivera has written a tutorial on how to customize the
    wordpress error
    page&lt;/a&gt;,
    note that it involves editing a file outside of wp-content, that
    means you'll have to re-do this "hack" every time you upgrade
    wordpress.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;PHPBB: Setting a custom error page on that is really easy, first
    create a php page displaying your message. Then at the bottom of
    /path/to/phpbb-install/includes/db.php you'll see&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Make the database connection.
$db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, false);
if(!$db-&amp;gt;db_connect_id)
{
message_die(CRITICAL_ERROR, "Could not connect to the database");
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;change it to&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; // Make the database connection.
$db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, false);
if(!$db-&amp;gt;db_connect_id)
{
 include("/path/to/my-custom-error-page.php");
        die();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now if you database dies, for the time it's down (&lt;em&gt;before cron fixes
it&lt;/em&gt;) wordpress &amp;amp; phpbb sites would get a much prettier error message.
Obviously there's no solution for apache as there's nothing to serve the
pages, but hopefully this kind of thing doesn't happen to often :D&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">nick</dc:creator><pubDate>Tue, 06 Mar 2007 13:32:00 +0000</pubDate><guid isPermaLink="false">tag:www.linickx.com,2007-03-06:service-recovery-scripts-error-page-tips</guid><category>apache</category><category>Enterprise Linux</category><category>Fedora</category><category>httpd</category><category>Linux</category><category>monitoring</category><category>mysql</category><category>recovery</category><category>WordPress</category></item></channel></rss>