Create a new file /etc/init.d/functions containing the following:
cat > functions << "EOF"
#!/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=`/usr/bin/basename $1`
pidlist=`/bin/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=`/usr/bin/basename $1`
if [ "$2" != "" ]
then
killlevel=$2
else
nolevel=1
fi
pidlist=`/bin/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
/bin/kill -TERM $pid
if ps h $pid >/dev/null 2>&1
then
/bin/kill -KILL $pid
fi
/bin/ps h $pid >/dev/null 2>&1
if [ $? = 0 ]
then
print_status failure
else
/bin/rm -f /var/run/$base.pid
print_status success
fi
else
/bin/kill $killlevel $pid
/bin/ps h $pid >/dev/null 2>&1
if [ $? = 0 ]
then
print_status failure
else
/bin/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=`/usr/bin/basename $1`
if [ -n "$2" ]
then
killlevel=-$2
else
nolevel=1
fi
pidlist=`/bin/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
/bin/kill -SIGHUP $pid
evaluate_retval
else
/bin/kill -$killlevel $pid
evaluate_retval
fi
else
print_status failure
fi
}
statusproc()
{
if [ $# = 0 ]
then
echo "Usage: status {program}"
return 1
fi
pid=`/bin/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=`/usr/bin/head -1 /var/run/$1.pid`
if [ -n "$pid" ]
then
echo "$1 not running but /var/run/$1.pid exists"
return 1
fi
else
echo "$i is not running"
fi
}
# End /etc/init.d/functions
EOF