Init script for Graylog-Server – CentOS 7 / RHEL 7

Graylog Logo
Graylog

If you have reached this page from my previous article on How to install Graylog2 on CentOS 7 / RHEL 7, you do not have to modify the below init script; just copy and paste content to graylog2 init file.

Create a init script file.

# vi /etc/init.d/graylog2

Place the following content in it, if your graylog-server is installed on different location other than /opt/graylog, modify the GRAYLOGCTL variable.

#!/bin/bash
### 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

CMD=$1
NOHUP=${NOHUP:=$(which nohup)}
PS=${PS:=$(which ps)}

# default java
JAVA_CMD=${JAVA_CMD:=$(which java)}

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

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

die() {
echo $*
exit 1
}

if [ -n "$JAVA_HOME" ]
then
# try to use $JAVA_HOME
if [ -x "$JAVA_HOME"/bin/java ]
then
JAVA_CMD="$JAVA_HOME"/bin/java
else
die "$JAVA_HOME"/bin/java is not executable
fi
fi

# resolve links - $0 may be a softlink
#GRAYLOGCTL="$0"
GRAYLOGCTL=/opt/graylog/  ##Path to Graylog-Server directory

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

# take variables from environment if set
GRAYLOGCTL_DIR=${GRAYLOGCTL_DIR:=$(dirname "$GRAYLOGCTL")}
GRAYLOG_SERVER_JAR=$GRAYLOGCTL/graylog.jar
GRAYLOG_CONF=${GRAYLOG_CONF:=/etc/graylog/server/server.conf}
GRAYLOG_PID=${GRAYLOG_PID:=/tmp/graylog.pid}
LOG_FILE=${LOG_FILE:=$GRAYLOGCTL/log/graylog-server.log}
LOG4J=${LOG4J:=}
DEFAULT_JAVA_OPTS="-Djava.library.path=${GRAYLOGCTL_DIR}/../lib/sigar -Xms1g -Xmx1g -XX:NewRatio=1 -XX:PermSize=128m -XX:MaxPermSize=256m -server -XX:+ResizeTLAB -XX:+UseConcMarkSweepGC -XX:+CMSConcurrentMTEnabled -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC -XX:-OmitStackTraceInFastThrow"
JAVA_OPTS="${JAVA_OPTS:="$DEFAULT_JAVA_OPTS"}"

start() {
echo "Starting graylog-server ..."
cd "$GRAYLOGCTL_DIR/.."
"${NOHUP}" "${JAVA_CMD}" ${JAVA_OPTS} ${LOG4J} -jar "${GRAYLOG_SERVER_JAR}" server -f "${GRAYLOG_CONF}" -p "${GRAYLOG_PID}" >> "${LOG_FILE}" 2>> "${LOG_FILE}" &
}

run() {
echo "Running graylog-server ..."
cd "$GRAYLOGCTL_DIR/.."
exec "${JAVA_CMD}" ${JAVA_OPTS} ${LOG4J} -jar "${GRAYLOG_SERVER_JAR}" server -f "${GRAYLOG_CONF}" -p "${GRAYLOG_PID}"
}

stop() {
if [ ! -f "${GRAYLOG_PID}" ]; then
die "Not stopping. PID file not found: ${GRAYLOG_PID}"
fi

PID=$(get_pid)

echo "Stopping graylog-server ($PID) ..."
echo "Waiting for graylog-server to halt."

kill $PID

while "$PS" -p $PID > /dev/null; do sleep 1; done;
rm -f "${GRAYLOG_PID}"

echo "graylog-server stopped"
}

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

status() {
PID=$(get_pid)
if [ ! -z $PID ]; then
if pid_running $PID; then
echo "graylog-server running with PID ${PID}"
return 0
else
rm "${GRAYLOG_PID}"
die "Removed stale PID file ${GRAYLOG_PID} with ${PID}."
fi
fi

die "graylog-server not running"
}

case "$CMD" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
run)
run
;;
*)
echo "Usage $0 {start|stop|restart|status|run}"
esac

Change the permission of the file.

# chmod 755 /etc/init.d/graylog2

Enable the autostart.

# chkconfig --add graylog2

# chkconfig graylog2 on

You can start and stop the graylog service using the following commands.

# service graylog2 start

# service graylog2 stop

That’s All!.

Credit: Modified from original graylogctl file.

POSTS YOU MAY LIKE -:)

Share This Post

Shares