Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
init script for graylog2
#! /bin/sh
### BEGIN INIT INFO
# Provides: Graylog2
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts Graylog2
# Description: Starts Graylog2 using start-stop-daemon
### END INIT INFO
GL_HOME=/opt/local/graylog2-server
GL_JAR=$GL_HOME/graylog2-server.jar
NAME=graylog2
DESC=graylog2
PID_FILE=/tmp/graylog2.pid
CONFIG_FILE=/etc/graylog2.conf
test -f $GL_JAR || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC... "
/usr/bin/java -jar "$GL_JAR" -f "$CONFIG_FILE" &
echo "started (`cat "$PID_FILE"`)."
;;
stop)
PID=`cat "$PID_FILE"`
echo -n "Stopping $DESC ($PID)..."
kill $PID
echo stopped.
;;
restart|force-reload)
${0} stop
sleep 0.5
${0} start
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0

My script, modified from the 0.10.0-rc.1.
I hope it can help...!

#!/bin/bash

CMD=$1

# resolve links - $0 may be a softlink
GRAYLOG2CTL="$0"

while [ -h "$GRAYLOG2CTL" ]; do
  ls=`ls -ld "$GRAYLOG2CTL"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    GRAYLOG2CTL="$link"
  else
    GRAYLOG2CTL=`dirname "$GRAYLOG2CTL"`/"$link"
  fi
done

GRAYLOG2CTL_DIR=`dirname "$GRAYLOG2CTL"`
GRAYLOG2_SERVER_JAR=${GRAYLOG2CTL_DIR}/../graylog2-server.jar
GRAYLOG2_CONFIG_SH=${GRAYLOG2CTL_DIR}/graylog2_config.sh
GRAYLOG2_CONF=/etc/graylog2.conf
GRAYLOG2_PID=/tmp/graylog2.pid

[ -f $GRAYLOG2_CONFIG_SH ] && . $GRAYLOG2_CONFIG_SH

start() {
    echo "Starting Graylog2-server ..."
    #$NOHUP java -jar ${GRAYLOG2_SERVER_JAR} -f ${GRAYLOG2_CONF} -p ${GRAYLOG2_PID} &
    java -jar ${GRAYLOG2_SERVER_JAR} -f ${GRAYLOG2_CONF} -p ${GRAYLOG2_PID} & > /dev/null 2>&1
}

stop() {
  pid=$(get_pid)
  if [[ -f $GRAYLOG2_PID && ! -z $pid ]]; then
    if pid_running $pid && kill $pid; then
      mess="stoped!"
      rm $GRAYLOG2_PID
      echo "Graylog2-server stoped!"
    fi
  else
    echo "Graylog2-server is not running!"
  fi
}

restart() {
    echo "Restarting Graylog2-server ..."
    stop
    start
}

status() {
    pid=$(get_pid)
    if [ ! -z $pid ]; then
        if pid_running $pid; then
            echo "Graylog2-server running as pid $pid"
            return 0
        else
            echo "Stale pid file with $pid - removing..."
            rm ${GRAYLOG2_PID}
        fi
    fi

    echo "Graylog2-server is not running"
}

get_pid() {
    cat ${GRAYLOG2_PID} 2> /dev/null
}

pid_running() {
    kill -0 $1 2> /dev/null
}

case "$CMD" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    *)
        echo "Usage $0 {start|stop|restart|status}"
        RETVAL=1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment