Created
June 3, 2011 22:37
-
-
Save kjoconnor/1007303 to your computer and use it in GitHub Desktop.
Logstash Agent init script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# | |
# Logstash Start/Stop logstash | |
# | |
# chkconfig: 345 99 99 | |
# description: Logstash | |
# processname: logstash | |
logstash_bin="java -Djava.net.preferIPv4Stack=true -jar /opt/logstash/logstash-1.0.11pre-monolithic.jar" | |
logstash_log="/opt/logstash/logstash-agent.log" | |
logstash_conf="/etc/logstash/logstash.conf" | |
NICE_LEVEL="-n 19" | |
PID_FILE="/opt/logstash/logstash-agent.pid" | |
find_logstash_process () { | |
if [ -f $PID_FILE ]; then | |
PID=`cat $PID_FILE` | |
else | |
PIDTEMP=`ps ux | grep logstash | grep java | grep agent | awk '{ print $2 }'` | |
# Pid not found | |
if [ "x$PIDTEMP" = "x" ]; then | |
PID=-1 | |
else | |
PID=$PIDTEMP | |
echo "Warning: no PID file found, replacing" | |
# Recompose PID file, not sure if there's a better way of handling this | |
echo $PID > $PID_FILE | |
fi | |
fi | |
} | |
start () { | |
LOG_DIR=`dirname ${logstash_log}` | |
if [ ! -d $LOG_DIR ]; then | |
echo "Log dir ${LOG_DIR} doesn't exist. Creating" | |
mkdir $LOG_DIR | |
fi | |
nohup nice ${NICE_LEVEL} ${logstash_bin} agent -f ${logstash_conf} > ${logstash_log} & | |
sleep 1 | |
PID=`ps ux | grep logstash | grep java | grep agent | awk '{ print $2}'` | |
if [ "x$PID" = "x" ]; then | |
PID=-1 | |
fi | |
if [ $PID -eq -1 ]; then | |
echo "Logstash failed to start." | |
exit 1 | |
else | |
echo $PID > $PID_FILE | |
echo "Started successfully, PID $PID" | |
exit 0 | |
fi | |
} | |
stop () { | |
find_logstash_process | |
if [ "$PID" -ne "-1" ]; then | |
kill $PID | |
if [ "$?" -eq "0" ]; then | |
echo "Logstash agent stopped successfully." | |
rm -f $PID_FILE | |
else | |
echo "Logstash was not stopped." | |
fi | |
else | |
echo "Couldn't find Logstash PID, is it running?" | |
fi | |
} | |
case $1 in | |
start) | |
start | |
;; | |
stop) | |
stop | |
exit 0 | |
;; | |
reload) | |
stop | |
start | |
;; | |
restart) | |
stop | |
start | |
;; | |
status) | |
find_logstash_process | |
if [ $PID -gt 0 ]; then | |
if [ -d /proc/$PID ]; then | |
echo "Running, PID $PID" | |
exit 0 | |
else | |
echo "Found PID file, but process does not exist. Removing stale PID." | |
rm -f $PID_FILE | |
fi | |
else | |
echo "Not running." | |
exit 1 | |
fi | |
;; | |
*) | |
echo $"Usage: $0 {start|stop|restart|reload|status}" | |
RETVAL=1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment