Skip to content

Instantly share code, notes, and snippets.

@mcorsen
Created May 13, 2014 23:39
Show Gist options
  • Save mcorsen/3ca7e5eff9c13f67e6f6 to your computer and use it in GitHub Desktop.
Save mcorsen/3ca7e5eff9c13f67e6f6 to your computer and use it in GitHub Desktop.
LSB compliant init script that actually works. Tested on Linux Mint 15 and CentOS 6.5.
#!/bin/bash
#
# chkconfig: 2345 90 12
# description: Description of service here
#
### BEGIN INIT INFO
# Provides: Short service name here
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 6
# Short-Description:
# Description:
### END INIT INFO
. /lib/lsb/init-functions
SERVICE=Short service name here
DAEMON=Full path to service script or executable here
PIDFILE=/var/run/${SERVICE}.pid
LOGFILE=/logs/${SERVICE}.log
# time to wait before checking for a successful start
STARTWAIT=.2s
showLogFile() {
echo "Log file ${LOGFILE} says:"
tail ${LOGFILE}
}
start() {
PID=`pidofproc -p $PIDFILE`
if [[ $? -eq 0 ]]; then
log_warning_msg "$SERVICE is already running."
exit
fi
${DAEMON} >> $LOGFILE 2>&1 &
PID=$!
if [[ ! "${PID}" ]]; then
log_failure_msg "Unable to start $SERVICE"
showLogFile
exit 1
fi
echo ${PID} > ${PIDFILE}
sleep ${STARTWAIT}
CHECK_PID=`pidofproc -p $PIDFILE`
if [[ $? -eq 0 ]]; then
log_success_msg "${SERVICE} server startup - pid $PID"
else
log_failure_msg "${SERVICE} did not start"
showLogFile
exit 1
fi
}
stop() {
PID=`pidofproc -p $PIDFILE`
if [[ $? -ne 0 ]]; then
echo "$SERVICE is not running."
exit 1
fi
killproc -p $PIDFILE $DAEMON
if [[ $? -eq 0 ]]; then
log_success_msg "Stopped ${SERVICE}"
else
log_failure_msg "Failed to stop ${SERVICE}"
fi
}
status() {
PID=`pidofproc -p $PIDFILE`
if [[ $? -eq 0 ]]; then
echo "$SERVICE is running (${PID})."
else
echo "$SERVICE is not running."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart|reload|condrestart)
stop
sleep .1s
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment