Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Updated to more closely follow Linux Standard Build (lsb; http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html) practices as well as more clearly abstract the function of the agent to support separate scripts for shippers, indexers, etc.
#!/bin/bash
#
# logstash Startup script for logstash
# chkconfig: 2345 20 80
# description: Logstash is a log shipping, indexing, and collocation tool.
# processname: java
### 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: logstash is a tool for managing events and logs. You can use it
# to collect logs, parse them, and store them for later use (like,i
# for searching).
# X-Author: Brian Harrington <redbeard@redhat.com>
### END INIT INFO
# Amount of memory for Java
JAVAMEM=256M
JAVA="/etc/alternatives/java"
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# We use name to allow for running multiple instances of logstash, thus
# supporting copying this file and supporting separate pids for shipper, web,
# indexer, etc.
name="logstash-shipper"
JARNAME=logstash.jar
#LOGSTASH_USER="logstash"
LOCATION=/opt/logstash
# Mostly auto configured variables
DESC=`echo $name | tr - " "`
CONFIG="/etc/logstash/${name:-'logstash'}.conf"
LOGFILE="/var/log/${name:-'logstash'}.log"
ARGS="-Xmx$JAVAMEM -Xms$JAVAMEM -jar ${LOCATION}/${JARNAME} agent --config ${CONFIG} --log ${LOGFILE}"
LOCKFILE=/var/lock/subsys/${name:-'logstash'}
PIDFILE=/var/run/${name:-'logstash'}.pid
. /etc/init.d/functions
if [ -r /usr/share/java-utils/java-functions ]; then
. /usr/share/java-utils/java-functions
else
echo "Can't read Java functions library (provided by jpackage-utils)"
echo "aborting"
exit 1
fi
RETVAL="0"
#
# Function that starts the daemon/service
#
do_start()
{
if [ "$RETVAL" != "0" ]; then
failure
return
fi
if [ -f ${LOCKFILE} ]; then
if [ -f ${PIDFILE} ]; then
read kpid < ${PIDFILE}
if [ -d "/proc/${kpid}" ]; then
warning "lockfile exists, but no process"
RETVAL="0"
return
fi
else
warning "application is already running"
return
fi
fi
( $JAVA $ARGS &) > /dev/null 2>&1 && success || failure
RETVAL=$?
PIDNUM=`pgrep -f "$JAVA[ ]*$ARGS"`
if [ "x${PIDNUM}" != "x" ]; then
echo ${PIDNUM} > ${PIDFILE}
chown ${LOGSTASH_USER:-'root'}:${LOGSTASH_GROUP:-'root'} $PIDFILE
else
warning "did not return a process number"
return
fi
if [ "$RETVAL" -ne "0" ]; then
failure "Error code ${RETVAL}"
fi
}
#
# 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
;;
stop)
echo -n "Stopping $DESC: "
do_stop
;;
restart|reload)
echo -n "Restarting $DESC: "
do_stop
do_start
;;
status)
status -p $PIDFILE $name
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
exit 3
;;
esac
echo
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment