Skip to content

Instantly share code, notes, and snippets.

@johanek
Last active December 11, 2015 08:29
Show Gist options
  • Save johanek/4573860 to your computer and use it in GitHub Desktop.
Save johanek/4573860 to your computer and use it in GitHub Desktop.
Generic init script
#!/bin/sh
#
# MYNAME DESCRIPTION
#
# chkconfig: 345 95 5
# description: DESCRIPTION
#
### BEGIN INIT INFO
# Provides: NAME
# Required-Start: $network
# Required-Stop: $network
# Description: DESCRIPTION
# Short-Description: DESCRIPTION
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=MYNAME
CONTROL=/path/to/${NAME}
DESC=MYNAME
LOCK_FILE=/var/lock/subsys/$NAME
RETVAL=0
set -e
start () {
set +e
OUTPUT=$($CONTROL start)
echo $OUTPUT | grep -q 'ERROR'
STARTSTATUS=$?
set -e
case "$STARTSTATUS" in
1)
echo SUCCESS
if [ -n "$LOCK_FILE" ] ; then
touch $LOCK_FILE
fi
RETVAL=0
;;
*)
echo FAILED
echo $OUTPUT
RETVAL=1
;;
esac
}
stop () {
set +e
$CONTROL stop
RETVAL=$?
set -e
if [ $RETVAL = 0 ] ; then
echo SUCCESS
if [ -n "$LOCK_FILE" ] ; then
rm -f $LOCK_FILE
fi
else
echo FAILED
fi
}
status() {
set +e
OUTPUT=$($CONTROL status)
echo $OUTPUT | grep -q 'no instances running'
if [ $? = 0 ] ; then
RETVAL=3
fi
echo $OUTPUT
set -e
}
restart() {
stop
start
}
case "$1" in
start)
echo -n "Starting $DESC: "
start
;;
stop)
echo -n "Stopping $DESC: "
stop
;;
restart)
echo -n "Restarting $DESC: "
restart
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status|restart}" >&2
RETVAL=1
;;
esac
exit $RETVAL
@johanek
Copy link
Author

johanek commented Jan 19, 2013

I use this format a lot for ruby script that use daemons to run as a daemon.

It would be good to manage the PID stuff directly from this init script, and perhaps use redhat functions - like daemon control and colourised success/failure messages

@johanek
Copy link
Author

johanek commented Jan 27, 2014

Or just use supervisord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment