Skip to content

Instantly share code, notes, and snippets.

@jmcguirk
Created November 3, 2014 19:08
Show Gist options
  • Save jmcguirk/c8c22a443fb2f82f877c to your computer and use it in GitHub Desktop.
Save jmcguirk/c8c22a443fb2f82f877c to your computer and use it in GitHub Desktop.
GO init.d wrapper
#!/bin/sh
. /etc/rc.d/init.d/functions
USER="root"
DAEMON="/etc/rpcs/Main"
SCRIPT="$(basename $0)"
PIDFILE="/var/run/$SCRIPT.pid"
SHUTDOWN_WAIT=20
app_pid()
{
echo `ps -aefw | grep "$DAEMON" | grep -v " grep " | awk '{print $2}'`
}
do_start()
{
echo -n $"Starting $DAEMON: "
pid=$(app_pid)
if [ -n "$pid" ]
then
echo "App server is already running (pid: $pid)"
else
echo "App server is not running - starting it up!"
runuser -l "$USER" -c "$DAEMON &" && echo_success || echo_failure
pid=$(app_pid)
echo "App server started, runing at $pid"
echo $pid > ${PIDFILE}
RETVAL=$?
echo
[ $RETVAL -eq 0 ]
fi
return 0
}
do_wait()
{
echo "Waiting for process to exit";
pid=$(app_pid)
if [ -n "$pid" ]
then
let kwait=$SHUTDOWN_WAIT
count=0;
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo -n -e "\nwaiting for processes to exit";
sleep 1
let count=$count+1;
done
fi
return 0
}
do_stop()
{
echo -n $"Stopping $DAEMON: "
pid=`ps -aefw | grep "$DAEMON" | grep -v " grep " | awk '{print $2}'`
kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure
if [ -f ${PIDFILE} ]; then
rm ${PIDFILE}
fi
RETVAL=$?
echo
[ $RETVAL -eq 0 ]
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
forcestart)
do_stop
do_wait
do_start
;;
restart)
do_stop
do_wait
do_start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment