Skip to content

Instantly share code, notes, and snippets.

@jmarcos-cano
Created September 12, 2017 23:18
Show Gist options
  • Save jmarcos-cano/7873ff4cb9d7998a0138a67b83090c5e to your computer and use it in GitHub Desktop.
Save jmarcos-cano/7873ff4cb9d7998a0138a67b83090c5e to your computer and use it in GitHub Desktop.
generic initd file
#!/bin/bash
### BEGIN INIT INFO
# Provides: generic
# Required-Start: a
# Required-Stop: a
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: generic service
### END INIT INFO
# if you are lazy
#NAME=${0##*/}
NAME="generic"
fix_the_name(){
reg="^S[0-9][0-9]*"
if [[ $NAME =~ $reg ]];then
echo "fixing the name"
NAME=$(echo ${NAME//[0-9]/} |tr "S" " "|tr -d ' ')
echo $NAME
fi
}
fix_the_name
##FILES
PIDFILE=/var/run/$NAME.pid
STDOUT_LOG="/var/log/$NAME/$NAME.log"
STDERR_LOG="/var/log/$NAME/$NAME.err.log"
mkdir -p $(dirname $STDOUT_LOG)
mkdir -p $(dirname $STDERR_LOG)
DAEMON="/usr/bin/kafka-server-start /etc/kafka/server.properties "
running()
{
local PID=$(cat "$1" 2>/dev/null) || return 1
kill -0 "$PID" 2>/dev/null
}
case "$1" in
start)
printf "%-40s" "Starting $NAME..."
if [ -f "$PIDFILE" ]
then
PID=`cat $PIDFILE`
if running $PIDFILE ;then
echo "Already Running! in $PID"
exit 1
else
rm -f "$PIDFILE"
fi
fi
PID=`$DAEMON >> $STDOUT_LOG 2>> $STDERR_LOG & echo $!`
#"Saving PID" $PID " to " $PIDFILE
if [ -z $PID ]; then
printf "%s\n" "Fail"
else
echo $PID > $PIDFILE
printf "%s\n" "Ok $PID"
fi
;;
status)
printf "%-40s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
printf "%s\n" "Process dead but pidfile exists"
#echo 0
else
if [ "$2" == "v" ] || [ "$2" == "ps" ];then
echo
echo "Running on $PID"
ps aux|grep -v grep|grep $PID
else
echo
echo "Running on $PID"
fi
#echo 1
fi
else
printf "%s\n" "Service not running"
#echo 0
fi
;;
stop)
printf "%-40s" "Stopping $NAME "
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
kill $PID 2>/dev/null
if running $PIDFILE ;then
echo "Force Kill"
kill -9 $PID
fi
printf "%s\n" "Ok [$PID]"
rm -f $PIDFILE
else
printf "%s\n" "pidfile not found"
printf "%s\n" "Not running"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|status [ps]}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment