Last active
December 11, 2015 08:29
-
-
Save johanek/4573860 to your computer and use it in GitHub Desktop.
Generic init script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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