Logstash init script for CentOS / Redhat

Here is a working script to run logstash as a service on CentOs or Redhat.
You may configure it by changing the name of the config file (change the SUFFIX value)
Several script may be used with different values for this variable.

(For Ubuntu, see this post)


Prerequisites :

  • Logstash must be installed in /opt/logstash/logstash.jar
  • The configuration file must be in /etc/logstash/SUFFIX.conf

Tags:

2 Responses

  1. Luke says:

    Thanks :)

  2. Jotun says:

    Here is a Logstash 1.4.2 init script I got to work for Centos:

    #!/bin/sh
    #
    # /etc/rc.d/init.d/logstash
    #
    # Starts Logstash as a daemon
    #
    # chkconfig: 2345 20 80
    # description: Starts Logstash as a daemon
    # pidfile: /var/run/logstash-agent.pid

    ### BEGIN INIT INFO
    # Provides: logstash
    # Required-Start: $local_fs $remote_fs
    # Required-Stop: $local_fs $remote_fs
    # Default-Start: 2 3 4 5
    # Default-Stop: S 0 1 6
    # Short-Description: Logstash
    # Description: Starts Logstash as a daemon.
    # Modified originally from https://gist.github.com/2228905#file_logstash.sh

    ### END INIT INFO

    # Manage Logstash as an indexer or shipper service
    MODE="indexer"

    export HOME=/root/
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    DESC="Logstash Daemon"
    DAEMON=/opt/logstash-1.4.2/bin/logstash
    CONFIG_FILE=/etc/logstash/$MODE.conf
    LOGFILE=/var/log/logstash/$MODE.conf
    ARGS=" -f ${CONFIG_FILE} -l ${LOGFILE}"
    SCRIPTNAME=/etc/init.d/logstash_$MODE
    PIDFILE=/var/run/logstash.pid
    base=logstash_$MODE

    . /etc/init.d/functions

    #
    # Function that starts the daemon/service
    #
    do_start()
    {
    ($DAEMON $ARGS &) \
    && success || failure
    }

    set_pidfile()
    {
    pgrep -f "$DAEMON[[:space:]]*$ARGS" > $PIDFILE
    }

    #
    # Function that stops the daemon/service
    #
    do_stop()
    {
    pid=
    cat $PIDFILE
    if checkpid $pid 2>&1; then
    # TERM first, then KILL if not dead
    kill -TERM $pid >/dev/null 2>&1
    usleep 100000
    if checkpid $pid && sleep 1 &&
    checkpid $pid && sleep $delay &&
    checkpid $pid ; then
    kill -KILL $pid >/dev/null 2>&1
    usleep 100000
    fi
    fi
    checkpid $pid
    RC=$?
    [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"

    }

    case "$1" in
    start)
    echo -n "Starting $DESC: "
    do_start
    touch /var/lock/subsys/$base
    set_pidfile
    ;;
    stop)
    echo -n "Stopping $DESC: "
    do_stop
    rm /var/lock/subsys/$base
    rm $PIDFILE
    ;;
    restart|reload)
    echo -n "Restarting $DESC: "
    do_stop
    do_start
    touch /var/lock/subsys/$base
    set_pidfile
    ;;
    status)
    status -p $PIDFILE
    ;;
    *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
    exit 3
    ;;
    esac

    echo
    exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Crocoware technical blog
CyberChimps