Skip to content

Commit 2209d07

Browse files
committed
Finally fix the install_server.sh script.
Includes changes from a dozen bug reports and pull requests. Was tested on Ubuntu, Debian and CentOS.
1 parent cc11d10 commit 2209d07

File tree

3 files changed

+140
-73
lines changed

3 files changed

+140
-73
lines changed

README

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ it the proper way for a production system, we have a script doing this
130130
for Ubuntu and Debian systems:
131131

132132
% cd utils
133-
% ./install_server
133+
% ./install_server.sh
134134

135135
The script will ask you a few questions and will setup everything you need
136136
to run Redis properly as a background daemon that will start again on

utils/install_server.sh

+113-58
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,48 @@
3030
# this scripts should be run as root
3131

3232
die () {
33-
echo "ERROR: $1. Aborting!"
33+
echo "ERROR: $1. Aborting!"
3434
exit 1
3535
}
3636

37+
38+
#Absolute path to this script
39+
SCRIPT=$(readlink -f $0)
40+
#Absolute path this script is in
41+
SCRIPTPATH=$(dirname $SCRIPT)
42+
3743
#Initial defaults
3844
_REDIS_PORT=6379
3945

4046
echo "Welcome to the redis service installer"
41-
echo "This script will help you easily set up a running redis server
42-
43-
"
47+
echo "This script will help you easily set up a running redis server"
48+
echo
4449

45-
#check for root user TODO: replace this with a call to "id"
46-
if [ `whoami` != "root" ] ; then
50+
#check for root user
51+
if [ "$(id -u)" -ne 0 ] ; then
4752
echo "You must run this script as root. Sorry!"
4853
exit 1
4954
fi
5055

51-
5256
#Read the redis port
53-
read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
54-
if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then
57+
read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
58+
if ! echo $REDIS_PORT | egrep -q '^[0-9]+$' ; then
5559
echo "Selecting default: $_REDIS_PORT"
56-
REDIS_PORT=$_REDIS_PORT
60+
REDIS_PORT=$_REDIS_PORT
5761
fi
5862

5963
#read the redis config file
6064
_REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
6165
read -p "Please select the redis config file name [$_REDIS_CONFIG_FILE] " REDIS_CONFIG_FILE
62-
if [ !"$REDIS_CONFIG_FILE" ] ; then
66+
if [ -z "$REDIS_CONFIG_FILE" ] ; then
6367
REDIS_CONFIG_FILE=$_REDIS_CONFIG_FILE
6468
echo "Selected default - $REDIS_CONFIG_FILE"
6569
fi
66-
#try and create it
67-
mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
6870

6971
#read the redis log file path
7072
_REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
7173
read -p "Please select the redis log file name [$_REDIS_LOG_FILE] " REDIS_LOG_FILE
72-
if [ !"$REDIS_LOG_FILE" ] ; then
74+
if [ -z "$REDIS_LOG_FILE" ] ; then
7375
REDIS_LOG_FILE=$_REDIS_LOG_FILE
7476
echo "Selected default - $REDIS_LOG_FILE"
7577
fi
@@ -78,55 +80,71 @@ fi
7880
#get the redis data directory
7981
_REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
8082
read -p "Please select the data directory for this instance [$_REDIS_DATA_DIR] " REDIS_DATA_DIR
81-
if [ !"$REDIS_DATA_DIR" ] ; then
83+
if [ -z "$REDIS_DATA_DIR" ] ; then
8284
REDIS_DATA_DIR=$_REDIS_DATA_DIR
8385
echo "Selected default - $REDIS_DATA_DIR"
8486
fi
85-
mkdir -p $REDIS_DATA_DIR || die "Could not create redis data directory"
8687

8788
#get the redis executable path
88-
_REDIS_EXECUTABLE=`which redis-server`
89+
_REDIS_EXECUTABLE=`command -v redis-server`
8990
read -p "Please select the redis executable path [$_REDIS_EXECUTABLE] " REDIS_EXECUTABLE
90-
if [ ! -f "$REDIS_EXECUTABLE" ] ; then
91+
if [ ! -x "$REDIS_EXECUTABLE" ] ; then
9192
REDIS_EXECUTABLE=$_REDIS_EXECUTABLE
92-
93-
if [ ! -f "$REDIS_EXECUTABLE" ] ; then
93+
94+
if [ ! -x "$REDIS_EXECUTABLE" ] ; then
9495
echo "Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
9596
exit 1
9697
fi
97-
9898
fi
9999

100+
#check the default for redis cli
101+
CLI_EXEC=`command -v redis-cli`
102+
if [ -z "$CLI_EXEC" ] ; then
103+
CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
104+
fi
100105

101-
#render the tmplates
102-
TMP_FILE="/tmp/$REDIS_PORT.conf"
103-
DEFAULT_CONFIG="../redis.conf"
104-
INIT_TPL_FILE="./redis_init_script.tpl"
105-
INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
106-
PIDFILE="/var/run/redis_$REDIS_PORT.pid"
106+
echo "Selected config:"
107107

108+
echo "Port : $REDIS_PORT"
109+
echo "Config file : $REDIS_CONFIG_FILE"
110+
echo "Log file : $REDIS_LOG_FILE"
111+
echo "Data dir : $REDIS_DATA_DIR"
112+
echo "Executable : $REDIS_EXECUTABLE"
113+
echo "Cli Executable : $CLI_EXEC"
108114

115+
read -p "Is this ok? Then press ENTER to go on or Ctrl-C to abort." _UNUSED_
109116

110-
#check the default for redis cli
111-
CLI_EXEC=`which redis-cli`
112-
if [ ! "$CLI_EXEC" ] ; then
113-
CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
117+
mkdir -p `dirname "$REDIS_CONFIG_FILE"` || die "Could not create redis config directory"
118+
mkdir -p `dirname "$REDIS_LOG_FILE"` || die "Could not create redis log dir"
119+
mkdir -p "$REDIS_DATA_DIR" || die "Could not create redis data directory"
120+
121+
#render the templates
122+
TMP_FILE="/tmp/${REDIS_PORT}.conf"
123+
DEFAULT_CONFIG="${SCRIPTPATH}/../redis.conf"
124+
INIT_TPL_FILE="${SCRIPTPATH}/redis_init_script.tpl"
125+
INIT_SCRIPT_DEST="/etc/init.d/redis_${REDIS_PORT}"
126+
PIDFILE="/var/run/redis_${REDIS_PORT}.pid"
127+
128+
if [ ! -f "$DEFAULT_CONFIG" ]; then
129+
echo "Mmmmm... the default config is missing. Did you switch to the utils directory?"
130+
exit 1
114131
fi
115132

116133
#Generate config file from the default config file as template
117134
#changing only the stuff we're controlling from this script
118135
echo "## Generated by install_server.sh ##" > $TMP_FILE
119136

120-
SED_EXPR="s#^port [0-9]{4}\$#port ${REDIS_PORT}#;\
121-
s#^logfile .+\$#logfile ${REDIS_LOG_FILE}#;\
122-
s#^dir .+\$#dir ${REDIS_DATA_DIR}#;\
123-
s#^pidfile .+\$#pidfile ${PIDFILE}#;\
124-
s#^daemonize no\$#daemonize yes#;"
125-
echo $SED_EXPR
137+
read -r SED_EXPR <<-EOF
138+
s#^port [0-9]{4}\$#port ${REDIS_PORT}#; \
139+
s#^logfile .+\$#logfile ${REDIS_LOG_FILE}#; \
140+
s#^dir .+\$#dir ${REDIS_DATA_DIR}#; \
141+
s#^pidfile .+\$#pidfile ${PIDFILE}#; \
142+
s#^daemonize no\$#daemonize yes#;
143+
EOF
126144
sed -r "$SED_EXPR" $DEFAULT_CONFIG >> $TMP_FILE
127145

128146
#cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
129-
cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
147+
cp $TMP_FILE $REDIS_CONFIG_FILE || die "Could not write redis config file $REDIS_CONFIG_FILE"
130148

131149
#Generate sample script from template file
132150
rm -f $TMP_FILE
@@ -138,53 +156,90 @@ REDIS_INIT_HEADER=\
138156
#Configurations injected by install_server below....\n\n
139157
EXEC=$REDIS_EXECUTABLE\n
140158
CLIEXEC=$CLI_EXEC\n
141-
PIDFILE=$PIDFILE\n
159+
PIDFILE=\"$PIDFILE\"\n
142160
CONF=\"$REDIS_CONFIG_FILE\"\n\n
143161
REDISPORT=\"$REDIS_PORT\"\n\n
144162
###############\n\n"
145163

146164
REDIS_CHKCONFIG_INFO=\
147165
"# REDHAT chkconfig header\n\n
148166
# chkconfig: - 58 74\n
149-
# description: redis_6379 is the redis daemon.\n
167+
# description: redis_${REDIS_PORT} is the redis daemon.\n
150168
### BEGIN INIT INFO\n
151169
# Provides: redis_6379\n
152-
# Required-Start: $network $local_fs $remote_fs\n
153-
# Required-Stop: $network $local_fs $remote_fs\n
170+
# Required-Start: \$network \$local_fs \$remote_fs\n
171+
# Required-Stop: \$network \$local_fs \$remote_fs\n
154172
# Default-Start: 2 3 4 5\n
155173
# Default-Stop: 0 1 6\n
156-
# Should-Start: $syslog $named\n
157-
# Should-Stop: $syslog $named\n
158-
# Short-Description: start and stop redis_6379\n
174+
# Should-Start: \$syslog \$named\n
175+
# Should-Stop: \$syslog \$named\n
176+
# Short-Description: start and stop redis_${REDIS_PORT}\n
159177
# Description: Redis daemon\n
160178
### END INIT INFO\n\n"
161179

162-
if [ !`which chkconfig` ] ; then
163-
#combine the header and the template (which is actually a static footer)
164-
echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
165-
else
180+
if command -v chkconfig >/dev/null; then
166181
#if we're a box with chkconfig on it we want to include info for chkconfig
167-
echo -e $REDIS_INIT_HEADER $REDIS_CHKCONFIG_INFO > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
182+
echo "$REDIS_INIT_HEADER" "$REDIS_CHKCONFIG_INFO" > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
183+
else
184+
#combine the header and the template (which is actually a static footer)
185+
echo "$REDIS_INIT_HEADER" > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
168186
fi
169187

188+
###
189+
# Generate sample script from template file
190+
# - No need to check which system we are on. The init info are comments and
191+
# do not interfere with update_rc.d systems. Additionally:
192+
# Ubuntu/debian by default does not come with chkconfig, but does issue a
193+
# warning if init info is not available.
194+
195+
cat > ${TMP_FILE} <<EOT
196+
#/bin/sh
197+
#Configurations injected by install_server below....
198+
199+
EXEC=$REDIS_EXECUTABLE
200+
CLIEXEC=$CLI_EXEC
201+
PIDFILE=$PIDFILE
202+
CONF="$REDIS_CONFIG_FILE"
203+
REDISPORT="$REDIS_PORT"
204+
###############
205+
# SysV Init Information
206+
# chkconfig: - 58 74
207+
# description: redis_${REDIS_PORT} is the redis daemon.
208+
### BEGIN INIT INFO
209+
# Provides: redis_${REDIS_PORT}
210+
# Required-Start: \$network \$local_fs \$remote_fs
211+
# Required-Stop: \$network \$local_fs \$remote_fs
212+
# Default-Start: 2 3 4 5
213+
# Default-Stop: 0 1 6
214+
# Should-Start: \$syslog \$named
215+
# Should-Stop: \$syslog \$named
216+
# Short-Description: start and stop redis_${REDIS_PORT}
217+
# Description: Redis daemon
218+
### END INIT INFO
219+
220+
EOT
221+
cat ${INIT_TPL_FILE} >> ${TMP_FILE}
222+
170223
#copy to /etc/init.d
171-
cp -f $TMP_FILE $INIT_SCRIPT_DEST && chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
224+
cp $TMP_FILE $INIT_SCRIPT_DEST && \
225+
chmod +x $INIT_SCRIPT_DEST || die "Could not copy redis init script to $INIT_SCRIPT_DEST"
172226
echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
173227

174228
#Install the service
175229
echo "Installing service..."
176-
if [ !`which chkconfig` ] ; then
230+
if command -v chkconfig >/dev/null 2>&1; then
231+
# we're chkconfig, so lets add to chkconfig and put in runlevel 345
232+
chkconfig --add redis_${REDIS_PORT} && echo "Successfully added to chkconfig!"
233+
chkconfig --level 345 redis_${REDIS_PORT} on && echo "Successfully added to runlevels 345!"
234+
elif command -v update-rc.d >/dev/null 2>&1; then
177235
#if we're not a chkconfig box assume we're able to use update-rc.d
178-
update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
236+
update-rc.d redis_${REDIS_PORT} defaults && echo "Success!"
179237
else
180-
# we're chkconfig, so lets add to chkconfig and put in runlevel 345
181-
chkconfig --add redis_$REDIS_PORT && echo "Successfully added to chkconfig!"
182-
chkconfig --level 345 redis_$REDIS_PORT on && echo "Successfully added to runlevels 345!"
238+
echo "No supported init tool found."
183239
fi
184-
240+
185241
/etc/init.d/redis_$REDIS_PORT start || die "Failed starting service..."
186242

187243
#tada
188244
echo "Installation successful!"
189245
exit 0
190-

utils/redis_init_script.tpl

+26-14
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,41 @@ case "$1" in
33
start)
44
if [ -f $PIDFILE ]
55
then
6-
echo "$PIDFILE exists, process is already running or crashed"
6+
echo "$PIDFILE exists, process is already running or crashed"
77
else
8-
echo "Starting Redis server..."
9-
$EXEC $CONF
8+
echo "Starting Redis server..."
9+
$EXEC $CONF
1010
fi
1111
;;
1212
stop)
1313
if [ ! -f $PIDFILE ]
1414
then
15-
echo "$PIDFILE does not exist, process is not running"
15+
echo "$PIDFILE does not exist, process is not running"
1616
else
17-
PID=$(cat $PIDFILE)
18-
echo "Stopping ..."
19-
$CLIEXEC -p $REDISPORT shutdown
20-
while [ -x /proc/${PID} ]
21-
do
22-
echo "Waiting for Redis to shutdown ..."
23-
sleep 1
24-
done
25-
echo "Redis stopped"
17+
PID=$(cat $PIDFILE)
18+
echo "Stopping ..."
19+
$CLIEXEC -p $REDISPORT shutdown
20+
while [ -x /proc/${PID} ]
21+
do
22+
echo "Waiting for Redis to shutdown ..."
23+
sleep 1
24+
done
25+
echo "Redis stopped"
2626
fi
2727
;;
28+
status)
29+
if [ ! -f $PIDFILE ]
30+
then
31+
echo 'Redis is not running'
32+
else
33+
echo "Redis is running ($(<$PIDFILE))"
34+
fi
35+
;;
36+
restart)
37+
$0 stop
38+
$0 start
39+
;;
2840
*)
29-
echo "Please use start or stop as first argument"
41+
echo "Please use start, stop, restart or status as first argument"
3042
;;
3143
esac

0 commit comments

Comments
 (0)