Skip to content

Instantly share code, notes, and snippets.

@polansky
Last active August 29, 2015 13:57
Show Gist options
  • Save polansky/9475745 to your computer and use it in GitHub Desktop.
Save polansky/9475745 to your computer and use it in GitHub Desktop.
Updating command to get PID. pgrep only works on the first 15 characters of a command name on CentOS 6. "logstash-forwarder" is 18. Doh!
#!/bin/sh
#
# /etc/init.d/logstash-forwarder
#
# Starts and stops logstash-forwarder as a daemon.
#
# chkconfig: 2345 30 70
# description: Starts and stops logstash-forwarder as a daemon.
# The name of this service
NAME=logstash-forwarder
### Start Configuration Options ###
# The JSON config to use
LJ_CONFIG=/etc/logstash-forwarder/logstash-forwarder.json
# The Lumberjack binary wrapper
LJ_BIN=`which logstash-forwarder`
# The Lumberjack runnning directory
LJ_RUNDIR="$(dirname ${LJ_BIN})/.."
# Any Lumberjack options
LJ_OPTS="-spool-size 1000"
# The log file for local info
LJ_LOG=/var/log/logstash-forwarder/logstash-forwarder
# The PID file
PID_FILE=/var/run/logstash-forwarder
# The command to daemonize
DAEMON="nohup ${LJ_BIN} -config ${LJ_CONFIG} ${LJ_OPTS} >>${LJ_LOG} 2>&1 &"
### End Configuration Options ###
. /etc/init.d/functions
check_prereqs() {
if [ -z ${LJ_BIN} ]; then
echo "ERROR: Unable to locate Lumberjack binary, make sure it's installed."
exit 1
elif [ ! -f ${LJ_CONFIG} ]; then
echo "ERROR: LJ_CONFIG (${LJ_CONFIG}) doesn't exist."
exit 1
elif [ ! -d `dirname ${LJ_LOG}` ]; then
echo "ERROR: LJ_LOG parent directory (`dirname ${LJ_LOG}`) doesn't exist."
exit 1
elif [ ! -d ${LJ_RUNDIR} ]; then
echo "ERROR: LJ_RUNDIR (${LJ_RUNDIR}) doesn't exist."
exit 1
fi
}
start() {
check_prereqs
echo -n $"Starting $NAME: "
cd $LJ_RUNDIR && daemon --check $NAME --pidfile $PID_FILE $DAEMON
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo_failure
echo
else
PID=$(pgrep ${NAME:0:15})
echo -n $PID > $PID_FILE
echo_success
echo
fi
return $RETVAL
}
stop () {
echo -n $"Stopping $NAME: "
killproc -p $PID_FILE $NAME
RETVAL=$?
echo
return $RETVAL
}
restart () {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $PID_FILE $NAME
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 2
;;
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment