#!/bin/sh
# chkconfig: 2345 55 25
# description: Redis Service

### BEGIN INIT INFO
# Provides:          Redis
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts Redis
# Description:       starts the BT-Web
### END INIT INFO

# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/www/server/redis/src/redis-server
CLIEXEC=/www/server/redis/src/redis-cli

PIDFILE=/var/run/redis_$REDISPORT.pid
CONF="/www/server/redis/redis.conf"

redis_start(){
	if [ -f $PIDFILE ]
	then
			echo "$PIDFILE exists, process is already running or crashed"
	else
			echo "Starting Redis server..."
			nohup $EXEC $CONF >> /www/server/redis/logs.pl 2>&1 &
	fi
}
redis_stop(){
	if [ ! -f $PIDFILE ]
	then
			echo "$PIDFILE does not exist, process is not running"
	else
			PID=$(cat $PIDFILE)
			echo "Stopping ..."
			$CLIEXEC -p $REDISPORT shutdown
			while [ -x /proc/${PID} ]
			do
				echo "Waiting for Redis to shutdown ..."
				sleep 1
			done
			echo "Redis stopped"
	fi
}


case "$1" in
    start)
		redis_start
        ;;
    stop)
        redis_stop
        ;;
	restart|reload)
		redis_stop
		sleep 0.3
		redis_start
		;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

