Search code examples
bashredhatdaemon

Running logstash forwarder as a daemon service


I found this article who explain how to make a start stop service ; http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html

And so I wrote this :

#!/bin/bash
#
# chkconfig: 3 80 20
# description: boop-logstash-forwarder
#
# Get function from functions library
. /etc/init.d/functions
# Start the service

LOGSTASH_FORWARDER="/logiciels/logstash-forwarder/logstash-forwarder"
LF_CONF="/appli/projects/BOOP-LOGSTASH-FORWARDER/logstash-forwarder.conf"
SERVICE_NAME="boop-logstash-forwarder"

start() {
        initlog -c "echo -n Starting $SERVICE_NAME: "
        $LOGSTASH_FORWARDER -config=$LF_CONF &
        ### Create the lock file ###
        touch /var/lock/subsys/$SERVICE_NAME
        success $"$SERVICE_NAME startup"
        echo
}
# Restart the service
stop() {
        initlog -c "echo -n Stopping $SERVICE_NAME: "
        killproc $SERVICE_NAME
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/$SERVICE_NAME
        echo
}
### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status $SERVICE_NAME
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
exit 0

I have 2 problem with this, first the logstash forwarder is taking control of the console an is very verbose, should I in the launch command redirect it's output to a file?

second and biggest problem the stop command won't work, I get :

Stopping looping-logstash-forwarder:                       [FAILED]

Did I do something wront in the script?

Thanks.

ps : Red Hat Enterprise Linux Server release 5.9


Solution

  • Here is a working vesion who uses kill instead of killproc :

    #!/bin/bash
    #
    # chkconfig: 3 80 20
    # description: boop-logstash-forwarder
    #
    # Get function from functions library
    . /etc/init.d/functions
    # Start the service
    
    LOGSTASH_FORWARDER="/logiciels/logstash-forwarder/logstash-forwarder"
    LF_CONF="/appli/projects/BOOP-LOGSTASH-FORWARDER/logstash-forwarder.conf"
    LOGFILE="/appli/projects/BOOP-LOGSTASH-FORWARDER/logstash-forwarder.log"
    PIDFILE="/appli/projects/BOOP-LOGSTASH-FORWARDER/logstash-forwarder.pid"
    SERVICE_NAME="boop-logstash-forwarder"
    
    blockUntilFileExist () {
        while ! [[ -f "$1" ]] ; do
            echo "Waiting for creation of $1"
            sleep 1
        done
    }
    
    start() {
            initlog -c "echo -n Starting $SERVICE_NAME: "
            $LOGSTASH_FORWARDER -config=$LF_CONF > $LOGFILE 2>&1 & echo $! > $PIDFILE
            blockUntilFileExist $PIDFILE
            success $"$SERVICE_NAME startup"
            echo
    }
    
    stop() {
            initlog -c "echo -n Stopping $SERVICE_NAME: "
            if [ ! -f $PIDFILE ]; then
                echo "process with pid contained in $PIDFILE does not exist"
                exit
            else
                echo "killing process with pid contained in $PIDFILE"
                kill `cat $PIDFILE`
            fi
            echo
    }
    ### main logic ###
    case "$1" in
      start)
            start
            ;;
      stop)
            stop
            ;;
      status)
            status $SERVICE_NAME
            ;;
      restart|reload|condrestart)1
            stop
            start
            ;;
      *)
            echo $"Usage: $0 {start|stop|restart|reload|status}"
            exit 1
    esac
    exit 0