Created
December 29, 2014 22:18
-
-
Save KoltesDigital/e5011d54230be84c693a to your computer and use it in GitHub Desktop.
Init.d script for Redis (example with GitLab configuration). Retrieves the pid file and the port/socket connection from the configuration file. Sends SHUTDOWN to stop the server. Tested on Debian Wheezy.
This file contains 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/bash | |
### BEGIN INIT INFO | |
# Provides: redis-gitlab | |
# Required-Start: $syslog | |
# Required-Stop: $syslog | |
# Should-Start: $local_fs | |
# Should-Stop: $local_fs | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: redis-server - Persistent key-value db | |
# Description: redis-server - Persistent key-value db | |
### END INIT INFO | |
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin | |
DAEMON=/usr/local/bin/redis-server | |
CONFFILE=/home/git/redis.conf | |
NAME=redis-gitlab | |
DESC="Redis (GitLab)" | |
USER=git | |
[ -x $DAEMON ] || exit 0 | |
PIDFILE=$(grep -oP "^pidfile +\K(.+)$" $CONFFILE) | |
LISTENING_PORT=$(grep -oP "^port +\K([0-9]+)$" $CONFFILE) | |
if [[ -z $LISTENING_PORT ]] | |
then | |
LISTENING_PORT=6379 | |
elif [[ $LISTENING_PORT -eq 0 ]] | |
then | |
unset LISTENING_PORT | |
fi | |
LISTENING_SOCKET=$(grep -oP "^unixsocket +\K(.+)$" $CONFFILE) | |
[ -r /etc/default/$NAME ] && . /etc/default/$NAME | |
. /lib/init/vars.sh | |
. /lib/lsb/init-functions | |
do_start() | |
{ | |
# Return | |
# 0 if daemon has been started | |
# 1 if daemon was already running | |
# 2 if daemon could not be started | |
start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER --exec $DAEMON --test > /dev/null || return 1 | |
start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER --exec $DAEMON -- $CONFFILE || return 2 | |
} | |
do_stop() | |
{ | |
# Return | |
# 0 if daemon has been stopped | |
# 1 if daemon was already stopped | |
# 2 if daemon could not be stopped | |
# other if a failure occurred | |
[ -f $PIDFILE ] || return 1 | |
PID=$(cat $PIDFILE) | |
[ -x /proc/${PID} ] || return 1 | |
if [[ -n $LISTENING_PORT ]] | |
then | |
redis-cli -p $LISTENING_PORT SHUTDOWN | |
elif [[ -n $LISTENING_SOCKET ]] | |
then | |
redis-cli -s $LISTENING_SOCKET SHUTDOWN | |
else | |
return 2 | |
fi | |
IT=50 | |
while [ "$IT" -gt 0 -a -f $PIDFILE ] | |
do | |
IT=$((IT-1)) | |
sleep .1 | |
done | |
if [ "$IT" -gt 0 ] | |
then | |
return 0 | |
else | |
return 2 | |
fi | |
} | |
case "$1" in | |
start) | |
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" | |
do_start | |
case "$?" in | |
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; | |
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; | |
esac | |
;; | |
stop) | |
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" | |
do_stop | |
case "$?" in | |
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; | |
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; | |
esac | |
;; | |
status) | |
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? | |
;; | |
restart|force-reload) | |
log_daemon_msg "Restarting $DESC" "$NAME" | |
do_stop | |
case "$?" in | |
0|1) | |
do_start | |
case "$?" in | |
0) log_end_msg 0 ;; | |
1) log_end_msg 1 ;; # Old process is still running | |
*) log_end_msg 1 ;; # Failed to start | |
esac | |
;; | |
*) | |
# Failed to stop | |
log_end_msg 1 | |
;; | |
esac | |
;; | |
*) | |
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 | |
exit 3 | |
;; | |
esac | |
: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment