Creating the functions script

Create a new file /etc/init.d/functions containing the following:



#!/bin/sh
# Begin /etc/init.d/functions

COL=70
SET_COL="echo -en \\033[${COL}G"
NORMAL="echo -en \\033[0;39m"
SUCCESS="echo -en \\033[1;32m"
FAILURE="echo -en \\033[1;31m"

evaluate_retval()
{
        if [ $? = 0 ]
        then
                print_status success
        else
                print_status failure
        fi
}

print_status()
{
        if [ $# = 0 ]
        then
                echo "Usage: print_status {success|failure}"
                exit 1
        fi

        case "$1" in
                success)
                        $SET_COL
                        echo -n "[  "
                        $SUCCESS
                        echo -n "OK"
                        $NORMAL
                        echo "  ]"
                        ;;
                failure)
                        $SET_COL
                        echo -n "["
                        $FAILURE
                        echo -n "FAILED"
                        $NORMAL
                        echo "]"
                        ;;
        esac

}

loadproc()
{
        if [ $# = 0 ]
        then
                echo "Usage: loadproc {program}"
                exit 1
        fi

        base=`basename $1`

        pidlist=`pidof -o $$ -o $PPID -o %PPID -x $base`

        pid=""

        for apid in $pidlist
        do
                if [ -d /proc/$apid ]
                then
                        pid="$pid $apid"
                fi
        done

        if [ ! -n "$pid" ]
        then
                $*
                evaluate_retval
        else
                print_status failure
        fi

}

killproc()
{

        if [ $# = 0 ]
        then
                echo "Usage: killproc {program} [signal]"
                exit 1
        fi

        base=`basename $1`

        if [ "$2" != "" ]
        then
                killlevel=$2
        else
                nolevel=1
        fi

        pidlist=`pidof -o $$ -o $PPID -o %PPID -x $base`

        pid=""

        for apid in $pidlist
        do
                if [ -d /proc/$apid ]
                then
                        pid="$pid $apid"
                fi
        done

        if [ -n "$pid" ]
        then
                if [ "$nolevel" = 1 ]
                then
                        kill -TERM $pid
                        if ps h $pid >/dev/null 2>&1
                        then
                                kill -KILL $pid
                        fi
                        ps h $pid >/dev/null 2>&1
                        if [ $? = 0 ]
                        then
                                print_status failure
                        else
                                rm -f /var/run/$base.pid
                                print_status success
                        fi
                else
                        kill $killlevel $pid
                        ps h $pid >/dev/null 2>&1
                        if [ $? = 0 ]
                        then
                                print_status failure
                        else
                                rm -f /var/run/$base.pid
                                print_status success
                        fi
                fi
        else
                print_status failure
        fi
}

reloadproc()
{
        if [ $# = 0 ]
        then
                echo "Usage: reloadproc {program} [signal]"
                exit 1
        fi

        base=`basename $1`

        if [ -n "$2" ]
        then
                killlevel=$2
        else
                nolevel=1
        fi

        pidlist=`pidof -o $$ -o $PPID -o %PPID -x $base`

        pid=""

        for apid in $pidlist
        do
                if [ -d /proc/$apid ]
                then
                        pid="$pid $apid"
                fi
        done

        if [ -n "$pid" ]
        then
                if [ "$nolevel" = 1 ]
                then
                        kill -SIGHUP $pid
                        evaluate_retval
                else
                        kill $killlevel $pid
                        evaluate_retval
                fi
        else
                print_status failure
        fi
}

statusproc()
{
        if [ $# = 0 ]
        then
                echo "Usage: status {program}"
                return 1
        fi

        pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
        if [ -n "$pid" ]
        then
                echo "$1 running with Process ID $pid"
                return 0
        fi

        if [ -f /var/run/$1.pid ]
        then
                pid=`head -1 /var/run/$1.pid`
                if [ -n "$pid" ]
                then
                        echo "$1 not running but /var/run/$1.pid exists"
                        return 1
                fi
        fi

}

# End /etc/init.d/functions