Skip to content

Instantly share code, notes, and snippets.

@bayetovsky
Last active October 6, 2017 08:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayetovsky/1e82150c4dbfc99d449b925f212b7885 to your computer and use it in GitHub Desktop.
Save bayetovsky/1e82150c4dbfc99d449b925f212b7885 to your computer and use it in GitHub Desktop.
Bash script to control fluentd daemon
#!/bin/bash
FLUENT_HOME=PLACEHOLDER_PATH
PID_FILE=$FLUENT_HOME/fluent.pid
CONF_FILE=$FLUENT_HOME/config/fluent.conf
PLUGIN_PATH=$FLUENT_HOME/config/plugin
LOG_FILE=$FLUENT_HOME/log/fluent.log
PSNAME="fluentd --daemon"
F_USER=PLACEHOLDER_USERNAME
F_GROUP=PLACEHOLDER_GROUP
start()
{
PID=`pgrep -f "$PSNAME"`
if [ -z "$PID" ]; then
if [ -f $PID_FILE ]; then rm -f $PID_FILE; fi
else
echo "fluentd already started."
return 1
fi
echo -n "Starting fluentd: "
# Check if the user and group exist
getent passwd $F_USER > /dev/null 2>&1
VALID_USER=$?
getent group $F_GROUP > /dev/null 2>&1
VALID_GROUP=$?
if [ $VALID_USER -ne 0 ] ; then echo "User $F_USER doesnt exist. Please double check your configuration"; exit 1; fi
if [ $VALID_GROUP -ne 0 ] ; then echo "Group $F_GROUP doesnt exist. Please double check your configuration" ; exit 1; fi
# Load user change options if the target user/group doesnt match the current user/group
USER_OPTS=""
if [ ! $F_USER == `whoami` ] ; then USER_OPTS=$USER_OPTS"--user $F_USER " ; fi
if [ ! `id -g -n $F_USER` == $F_GROUP ] ; then USER_OPTS=$USER_OPTS"--group $F_GROUP"; fi
# Execute fluent
nice $PSNAME $PID_FILE $USER_OPTS --config $CONF_FILE --log $LOG_FILE -p $PLUGIN_PATH -v
echo "done."
}
stop()
{
PID=`pgrep -f "$PSNAME"`
if [ -z "$PID" ]; then
echo "fluentd already stopped."
return 0
fi
echo -n "Stopping fluentd: "
pkill -TERM -f "$PSNAME"
count=0
while [ ! -z "$PID" ]; do
count=`expr $count + 1`
if [ `expr $count % 10` -eq 0 ]; then pkill -KILL -f "$PSNAME"; fi
if [ $count -eq 60 ]; then
echo " failed."
return 1
fi
sleep 1
PID=`pgrep -f "$PSNAME"`
done
echo "done."
}
restart()
{
stop && start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
pkill -HUP -f "$PSNAME"
;;
flush)
pkill -USR1 -f "$PSNAME"
;;
*)
echo "Usage: $0 {start|stop|restart|reload|flush}"
exit 1
;;
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment