1

I am trying to make an init script to work as expected, the start / stop functionalities works flawlessly, but I cannot get the status function to work as expected. I am running : Distributor ID: Debian Description: Debian GNU/Linux 7.6 (wheezy) Release: 7.6 Codename: wheezy

Here is my init script :

#! /bin/sh
### BEGIN INIT INFO
# Provides:          graylog-collector
# Required-Start:    $network $named $remote_fs $syslog
# Required-Stop:     $network $named $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Graylog Collector
# Description:       Graylog's open source log shipper
### END INIT INFO

# Process name ( For display )
NAME="graylog-collector"
# Daemon name, where is the actual executable
DAEMON="/etc/graylog-collector/bin/graylog-collector"
# DAEMON args
DAEMON_OPT="run -f /etc/graylog-collector/config/collector.conf"
# User to run the deamon
DAEMON_USER="graylog-collector"
# pid file for the daemon
PIDDIR="/var/run/$NAME"
PIDFILE="$PIDDIR/$NAME.pid"


PATH="/sbin:/bin:/usr/sbin:/usr/bin"

test -x $DAEMON || exit 0

. /lib/lsb/init-functions




d_start () {
    log_daemon_msg "Starting system $NAME Daemon"
    if [ ! -e $PIDDIR ] ; then
            mkdir $PIDDIR
            chown ${DAEMON_USER}:${DAEMON_USER} $PIDDIR
    fi
    start-stop-daemon --background --start \
            --user $DAEMON_USER \
            --chuid $DAEMON_USER \
            --make-pidfile \
            --pidfile $PIDFILE \
            --startas /bin/bash -- -c "exec $DAEMON $DAEMON_OPT >> /var/log/graylog-collector/console.log 2>&1" || return 2
    sleep 2
    log_end_msg $?
}

d_stop () {
    log_daemon_msg "Stopping system $NAME Daemon"
    start-stop-daemon --stop --retry 5 --quiet --user $DAEMON_USER
    rm -f $PIDFILE
    log_end_msg $?
}

case "$1" in

    start|stop)
            d_${1}
            ;;

    restart|reload|force-reload)
                    d_stop
                    d_start
            ;;

    force-stop)
            d_stop
            killall -q $NAME || true
            sleep 2
            killall -q -9 $NAME || true
            ;;

    status)
            status_of_proc "${PIDFILE}" "$DAEMON" "$NAME" && exit 0 || exit $?
            ;;
    *)
            echo "Usage: /etc/init.d/$NAME {start|stop|force-stop|restart|reload|force-reload|status}"
            exit 1
            ;;
esac
exit 0

Thank you.

Update 1 : I updated the script with an ugly but working solution (at least for my little requirements), by creating this function :

do_status () {

        if { ps -U ${DAEMON_USER} ; } >/dev/null 2>&1 ; then

                log_success_msg "${NAME} is running"

                return 0

        else

                log_failure_msg "${NAME} is not running"

                return $?

        fi

}

Then calling it :

status)
                    #status_of_proc "${PIDFILE}" "$DAEMON" "$NAME" && exit 0     || exit $?
                    do_status
                    ;;
1

From my understanding you should have used -p with "${PIDFILE}". That said unless you used "set -e", which you should never do in an init.d script checking the process with -p doesn't work I think. In the end try this instead:

status)
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.