Skip to content

Instantly share code, notes, and snippets.

@higebu
Created May 28, 2015 07:37
Show Gist options
  • Save higebu/9be49308fd629a87b0a7 to your computer and use it in GitHub Desktop.
Save higebu/9be49308fd629a87b0a7 to your computer and use it in GitHub Desktop.
/etc/init.d/serf for ubuntu
#!/bin/bash
### BEGIN INIT INFO
# Provides: serf
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start serf daemon
# Description: Start serf daemon
### END INIT INFO
NAME=serf
SERF_BINARY=/usr/local/bin/$NAME
SERF_AGENT_LOG=/var/log/$NAME/serf.log
SERF_AGENT_CONF_FILE=/etc/$NAME/$NAME.conf
SERF_USER=root
SLEEP_TIME=5
CURRENT_WAIT=0
TIMEOUT=30
start() {
find_pid
if [ "" = "$FOUND_PID" ]; then
su -l $SERF_USER -c "$SERF_BINARY agent -config-file $SERF_AGENT_CONF_FILE >>$SERF_AGENT_LOG 2>&1 &"
if [[ $? -ne 0 ]]; then
echo "Error starting $NAME"
exit 1
fi
echo "$NAME successfully started"
else
echo "$NAME is already running"
fi
}
stop() {
find_pid
if [ -z "$FOUND_PID" ]; then
echo "$NAME is not running, nothing to stop"
else
while [[ -n $FOUND_PID ]];
do
echo "Attempting to shutdown $NAME..."
kill -INT $FOUND_PID
if [[ $? -ne 0 ]]; then
echo "Error stopping $NAME"
exit 1
fi
sleep $SLEEP_TIME
CURRENT_WAIT=$(($CURRENT_WAIT+$SLEEP_TIME))
if [[ $CURRENT_WAIT -gt $TIMEOUT ]]; then
echo "Timed out waiting for $NAME to stop"
exit 1
fi
find_pid
done
echo "Stopped $NAME"
fi
}
status() {
find_pid
if [ -z "$FOUND_PID" ]; then
echo "$NAME is not running" ; exit 1
else
echo "$NAME is running : $FOUND_PID"
fi
}
reload() {
find_pid
if [ -z "$FOUND_PID" ]; then
echo "$NAME is not running" ; exit 1
else
kill -HUP $FOUND_PID
echo "Reloaded $NAME"
fi
}
find_pid() {
FOUND_PID=`pgrep -f $SERF_BINARY`
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
reload)
reload
;;
*)
echo "Usage: $0 {start|stop|restart|status|reload}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment