Centos – daemon function not found in CentOS 6.6

centoscentos6daemon

I am trying to set init.d script for some service – let it be less
Here is my script:

#!/bin/bash -xv
# description: read service
#Source function library

if [ -x /etc/rc.d/init.d/functions ];then
. /etc/rc.d/init.d/functions
fi

RETVAL=0
LESS=/usr/bin/less
PIDFILE=/var/run/read.pid

start() {
echo -n $"Starting $LESS service: "
daemon /usr/bin/less
RETVAL=$?
echo $! > $PIDFILE; 
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/LESS
echo
return $RETVAL
}

stop() {
echo -n $"Shutdown $LESS service: "
killproc /usr/bin/less
rm -f $PIDFILE
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/LESS
echo
return $RETVAL
}

restart() {
echo -n $"Restarting $LESS service: "
killproc /usr/bin/less
daemon /usr/bin/less
}


case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
status)
    if [ `pidof /var/run/webreaderd.pid` ];then
        echo "Running"
    else
        echo "Not running"
    fi
    ;;
restart|reload)
    stop
    start
    ;;
*)
    echo $"Usage: $0 {start|stop|restart|reload|status}"
    exit 1
esac
exit $?

But while debugging /etc/init.d/read.sh startI got this error: line 15: daemon: command not found despite the fact that my . /etc/rc.d/init.d/functions is present and it is not empty. How to make my daemon function work?

Best Answer

The -x test is checking that the file exists and is executable (or in the case of a directory traverse/searchable).

Your functions file probably doesn't have execute permission on it.

In truth it doesn't need to have execute permissions on it as you are sourcing it into your current script. You can either include it without a test

. /etc/rc.d/init.d/functions

or include it if it exists

if [ -e /etc/rc.d/init.d/functions ];then
    . /etc/rc.d/init.d/functions
else
   echo "Some meaningful error message"
   exit 1
fi

You probably want to exit if it doesn't exist and you are using something it relies upon too.