Service Auto Recovery

Discussion in 'Tips/Tricks/Mods' started by linickx, Mar 6, 2007.

  1. linickx

    linickx Member

    Hi All,

    I thought I'd share this tip, I wanted a script that would restart httpd/mysql if the process died (full waffle here), written & tested on CentOS your mileage might vary elsewhere, but obviously it can easily be expanded to "recover" the full range of ISPConfig services.

    Save the following code in /etc/cron.hourly

    Code:
    #!/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 < /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
    If anyone has a cleaner / better solution I'd be glad to hear it :D
    Good Luck !
     
  2. falko

    falko Super Moderator Howtoforge Staff

  3. linickx

    linickx Member

    cool, thanks for the link, I'll check it out :)
     

Share This Page