Redis has a service install script install_server.sh. It takes a few prompts and adds a new Redis service on Linux. One issue I found when working with Redis passwords, the service script doesn’t handle start/stop correctly. In a prior post, I detail the installation process for Redis on Amazon Linux. Here’s a fix to the services script. The issue is with the stop. There is no password passed to the service script on shutdown. With passwords and no modification you are limited to doing the following:

service redis_6379 start
redis-cli -p 6379 -a YourPassword shutdown

The standard “service stop” command doesn’t work but here’s an update to the service script that could be implemented in the install_server.sh script by the Redis team.. My edits are tagged with #tlr and should be changed in the redis_6379 file in the /etc/init.d directory (or the origin install_server.sh script, if you want to get even more fancy. The install_server.sh script is used to create the redis_6379 start/stop script.)

#!/bin/sh
#Configurations injected by install_server below....

NAME=`basename ${0}` #tlr

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

#PIDFILE=/var/run/redis_6379.pid #tlr
#CONF="/etc/redis/6379.conf" #tlr
#REDISPORT="6379" #tlr

PIDFILE=/var/run/${NAME}.pid #tlr
CONF="/etc/redis/${NAME#*_}.conf" #tlr
REDISPORT="${NAME#*_}" #tlr

PassVar=$(grep "requirepass " $CONF | cut -d' ' -f1 | tr -d '\012\015') #tlr
# PassVar is the requirepass variable name (with or without the # comment) #tlr
if [ $PassVar = "requirepass" ] #tlr
then #tlr
 requirepass=$(grep "requirepass " $CONF | cut -d' ' -f2 | tr -d '\012\015') #tlr
else #tlr
 # password commented output #tlr
 requirepass="" #tlr
fi #tlr

###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO

case "$1" in
 start)
 if [ -f $PIDFILE ]
 then
 echo "$PIDFILE exists, process is already running or crashed"
 else

echo "never" > /sys/kernel/mm/transparent_hugepage/enabled #tlr
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag #tlr
# by addding hugpage here it overides other places set on reboot (ie.onAWS)

echo "Starting Redis server..."
 $EXEC $CONF
 fi
 ;;
 stop)
 if [ ! -f $PIDFILE ]
 then
 echo "$PIDFILE does not exist, process is not running"
 else
 PID=$(cat $PIDFILE)
 echo "Stopping ..."

# $CLIEXEC -p $REDISPORT shutdown #tlr

if [ -z $requirepass ] #tlr
then #tlr
 $CLIEXEC -p $REDISPORT shutdown #tlr
else #tlr
 echo "Using .conf File Password AUTH" #tlr
 $CLIEXEC -p $REDISPORT -a $requirepass shutdown #tlr
fi #tlr

while [ -x /proc/${PID} ]
 do
 echo "Waiting for Redis to shutdown ..."
 sleep 1
 done
 echo "Redis stopped"
 fi
 ;;
 status)
 PID=$(cat $PIDFILE)
 if [ ! -x /proc/${PID} ]
 then
 echo 'Redis is not running'
 else
 echo "Redis is running ($PID)"
 fi
 ;;
 restart)
 $0 stop
 $0 start
 ;;
 *)
 echo "Please use start, stop, restart or status as first argument"
 ;;
esac

These changes simply allow for multiple services by creating symbolic links for additional nodes to the original redis_6379 service start/stop script. Additional Redis node services could be added by doing the following

ln -s /etc/init.d/redis_6379 /etc/init.d/redis_6101

This creates a start / stop script for the additional node by linking to the original 6379 service start/stop script. You will need an additional service for each node (master or slave) that you are running on the instance. Also, duplicate and modify your /etc/redis/xxxx.conf file as needed. In a prior post, I detail the configuration for a Redis Cluster with Passwords. Finally, issue a command to add the service:

service --add redis_6101

You can now reboot the Linux instance and your additional services will still be running. You can now also use the command $ service redis_6101 restart without an error due to passwords.

Advertisements