Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
68a3af5f
Commit
68a3af5f
authored
Mar 24, 2014
by
Salvatore Sanfilippo
Browse files
Merge pull request #1609 from badboy/install_server-fix
Finally fix the `install_server.sh` script.
parents
93253c27
2209d077
Changes
3
Show whitespace changes
Inline
Side-by-side
README
View file @
68a3af5f
...
...
@@ -130,7 +130,7 @@ it the proper way for a production system, we have a script doing this
for Ubuntu and Debian systems:
% cd utils
% ./install_server
% ./install_server
.sh
The script will ask you a few questions and will setup everything you need
to run Redis properly as a background daemon that will start again on
...
...
utils/install_server.sh
View file @
68a3af5f
...
...
@@ -34,24 +34,28 @@ die () {
exit
1
}
#Absolute path to this script
SCRIPT
=
$(
readlink
-f
$0
)
#Absolute path this script is in
SCRIPTPATH
=
$(
dirname
$SCRIPT
)
#Initial defaults
_REDIS_PORT
=
6379
echo
"Welcome to the redis service installer"
echo
"This script will help you easily set up a running redis server
"
echo
"This script will help you easily set up a running redis server"
echo
#check for root user
TODO: replace this with a call to "id"
if
[
`
whoami
`
!=
"root"
]
;
then
#check for root user
if
[
"
$(
id
-u
)
"
-ne
0
]
;
then
echo
"You must run this script as root. Sorry!"
exit
1
fi
#Read the redis port
read
-p
"Please select the redis port for this instance: [
$_REDIS_PORT
] "
REDIS_PORT
if
[
!
`
echo
$REDIS_PORT
| egrep
"
^[0-9]+
\$
"
`
]
;
then
if
!
echo
$REDIS_PORT
| egrep
-q
'
^[0-9]+
$'
;
then
echo
"Selecting default:
$_REDIS_PORT
"
REDIS_PORT
=
$_REDIS_PORT
fi
...
...
@@ -59,17 +63,15 @@ fi
#read the redis config file
_REDIS_CONFIG_FILE
=
"/etc/redis/
$REDIS_PORT
.conf"
read
-p
"Please select the redis config file name [
$_REDIS_CONFIG_FILE
] "
REDIS_CONFIG_FILE
if
[
!
"
$REDIS_CONFIG_FILE
"
]
;
then
if
[
-z
"
$REDIS_CONFIG_FILE
"
]
;
then
REDIS_CONFIG_FILE
=
$_REDIS_CONFIG_FILE
echo
"Selected default -
$REDIS_CONFIG_FILE
"
fi
#try and create it
mkdir
-p
`
dirname
"
$REDIS_CONFIG_FILE
"
`
||
die
"Could not create redis config directory"
#read the redis log file path
_REDIS_LOG_FILE
=
"/var/log/redis_
$REDIS_PORT
.log"
read
-p
"Please select the redis log file name [
$_REDIS_LOG_FILE
] "
REDIS_LOG_FILE
if
[
!
"
$REDIS_LOG_FILE
"
]
;
then
if
[
-z
"
$REDIS_LOG_FILE
"
]
;
then
REDIS_LOG_FILE
=
$_REDIS_LOG_FILE
echo
"Selected default -
$REDIS_LOG_FILE
"
fi
...
...
@@ -78,55 +80,71 @@ fi
#get the redis data directory
_REDIS_DATA_DIR
=
"/var/lib/redis/
$REDIS_PORT
"
read
-p
"Please select the data directory for this instance [
$_REDIS_DATA_DIR
] "
REDIS_DATA_DIR
if
[
!
"
$REDIS_DATA_DIR
"
]
;
then
if
[
-z
"
$REDIS_DATA_DIR
"
]
;
then
REDIS_DATA_DIR
=
$_REDIS_DATA_DIR
echo
"Selected default -
$REDIS_DATA_DIR
"
fi
mkdir
-p
$REDIS_DATA_DIR
||
die
"Could not create redis data directory"
#get the redis executable path
_REDIS_EXECUTABLE
=
`
which
redis-server
`
_REDIS_EXECUTABLE
=
`
command
-v
redis-server
`
read
-p
"Please select the redis executable path [
$_REDIS_EXECUTABLE
] "
REDIS_EXECUTABLE
if
[
!
-
f
"
$REDIS_EXECUTABLE
"
]
;
then
if
[
!
-
x
"
$REDIS_EXECUTABLE
"
]
;
then
REDIS_EXECUTABLE
=
$_REDIS_EXECUTABLE
if
[
!
-
f
"
$REDIS_EXECUTABLE
"
]
;
then
if
[
!
-
x
"
$REDIS_EXECUTABLE
"
]
;
then
echo
"Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?"
exit
1
fi
fi
#check the default for redis cli
CLI_EXEC
=
`
command
-v
redis-cli
`
if
[
-z
"
$CLI_EXEC
"
]
;
then
CLI_EXEC
=
`
dirname
$REDIS_EXECUTABLE
`
"/redis-cli"
fi
#render the tmplates
TMP_FILE
=
"/tmp/
$REDIS_PORT
.conf"
DEFAULT_CONFIG
=
"../redis.conf"
INIT_TPL_FILE
=
"./redis_init_script.tpl"
INIT_SCRIPT_DEST
=
"/etc/init.d/redis_
$REDIS_PORT
"
PIDFILE
=
"/var/run/redis_
$REDIS_PORT
.pid"
echo
"Selected config:"
echo
"Port :
$REDIS_PORT
"
echo
"Config file :
$REDIS_CONFIG_FILE
"
echo
"Log file :
$REDIS_LOG_FILE
"
echo
"Data dir :
$REDIS_DATA_DIR
"
echo
"Executable :
$REDIS_EXECUTABLE
"
echo
"Cli Executable :
$CLI_EXEC
"
read
-p
"Is this ok? Then press ENTER to go on or Ctrl-C to abort."
_UNUSED_
#check the default for redis cli
CLI_EXEC
=
`
which redis-cli
`
if
[
!
"
$CLI_EXEC
"
]
;
then
CLI_EXEC
=
`
dirname
$REDIS_EXECUTABLE
`
"/redis-cli"
mkdir
-p
`
dirname
"
$REDIS_CONFIG_FILE
"
`
||
die
"Could not create redis config directory"
mkdir
-p
`
dirname
"
$REDIS_LOG_FILE
"
`
||
die
"Could not create redis log dir"
mkdir
-p
"
$REDIS_DATA_DIR
"
||
die
"Could not create redis data directory"
#render the templates
TMP_FILE
=
"/tmp/
${
REDIS_PORT
}
.conf"
DEFAULT_CONFIG
=
"
${
SCRIPTPATH
}
/../redis.conf"
INIT_TPL_FILE
=
"
${
SCRIPTPATH
}
/redis_init_script.tpl"
INIT_SCRIPT_DEST
=
"/etc/init.d/redis_
${
REDIS_PORT
}
"
PIDFILE
=
"/var/run/redis_
${
REDIS_PORT
}
.pid"
if
[
!
-f
"
$DEFAULT_CONFIG
"
]
;
then
echo
"Mmmmm... the default config is missing. Did you switch to the utils directory?"
exit
1
fi
#Generate config file from the default config file as template
#changing only the stuff we're controlling from this script
echo
"## Generated by install_server.sh ##"
>
$TMP_FILE
SED_EXPR
=
"s#^port [0-9]{4}
\$
#port
${
REDIS_PORT
}
#;
\
s#^logfile .+
\$
#logfile
${
REDIS_LOG_FILE
}
#;
\
s#^dir .+
\$
#dir
${
REDIS_DATA_DIR
}
#;
\
s#^pidfile .+
\$
#pidfile
${
PIDFILE
}
#;
\
s#^daemonize no
\$
#daemonize yes#;"
echo
$SED_EXPR
read
-r
SED_EXPR
<<-
EOF
s#^port [0-9]{4}
\$
#port
${
REDIS_PORT
}
#;
\
s#^logfile .+
\$
#logfile
${
REDIS_LOG_FILE
}
#;
\
s#^dir .+
\$
#dir
${
REDIS_DATA_DIR
}
#;
\
s#^pidfile .+
\$
#pidfile
${
PIDFILE
}
#;
\
s#^daemonize no
\$
#daemonize yes#;
EOF
sed
-r
"
$SED_EXPR
"
$DEFAULT_CONFIG
>>
$TMP_FILE
#cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
cp
-f
$TMP_FILE
$REDIS_CONFIG_FILE
||
exit
1
cp
$TMP_FILE
$REDIS_CONFIG_FILE
||
die
"Could not write redis config file
$REDIS_CONFIG_FILE
"
#Generate sample script from template file
rm
-f
$TMP_FILE
...
...
@@ -138,7 +156,7 @@ REDIS_INIT_HEADER=\
#Configurations injected by install_server below....
\n\n
EXEC=
$REDIS_EXECUTABLE
\n
CLIEXEC=
$CLI_EXEC
\n
PIDFILE=
$PIDFILE
\n
PIDFILE=
\"
$PIDFILE
\
"\
n
CONF=
\"
$REDIS_CONFIG_FILE
\"\n\n
REDISPORT=
\"
$REDIS_PORT
\"\n\n
###############
\n\n
"
...
...
@@ -146,40 +164,78 @@ REDISPORT=\"$REDIS_PORT\"\n\n
REDIS_CHKCONFIG_INFO
=
\
"# REDHAT chkconfig header
\n\n
# chkconfig: - 58 74
\n
# description: redis_
6379
is the redis daemon.
\n
# description: redis_
${
REDIS_PORT
}
is the redis daemon.
\n
### BEGIN INIT INFO
\n
# Provides: redis_6379
\n
# Required-Start:
$network
$local_fs
$remote_fs
\n
# Required-Stop:
$network
$local_fs
$remote_fs
\n
# Required-Start:
\
$
network
\
$
local_fs
\
$
remote_fs
\n
# Required-Stop:
\
$
network
\
$
local_fs
\
$
remote_fs
\n
# Default-Start: 2 3 4 5
\n
# Default-Stop: 0 1 6
\n
# Should-Start:
$syslog
$named
\n
# Should-Stop:
$syslog
$named
\n
# Short-Description: start and stop redis_
6379
\n
# Should-Start:
\
$
syslog
\
$
named
\n
# Should-Stop:
\
$
syslog
\
$
named
\n
# Short-Description: start and stop redis_
${
REDIS_PORT
}
\n
# Description: Redis daemon
\n
### END INIT INFO
\n\n
"
if
[
!
`
which chkconfig
`
]
;
then
#combine the header and the template (which is actually a static footer)
echo
$REDIS_INIT_HEADER
>
$TMP_FILE
&&
cat
$INIT_TPL_FILE
>>
$TMP_FILE
||
die
"Could not write init script to
$TMP_FILE
"
else
if
command
-v
chkconfig
>
/dev/null
;
then
#if we're a box with chkconfig on it we want to include info for chkconfig
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
"
echo
"
$REDIS_INIT_HEADER
"
"
$REDIS_CHKCONFIG_INFO
"
>
$TMP_FILE
&&
cat
$INIT_TPL_FILE
>>
$TMP_FILE
||
die
"Could not write init script to
$TMP_FILE
"
else
#combine the header and the template (which is actually a static footer)
echo
"
$REDIS_INIT_HEADER
"
>
$TMP_FILE
&&
cat
$INIT_TPL_FILE
>>
$TMP_FILE
||
die
"Could not write init script to
$TMP_FILE
"
fi
###
# Generate sample script from template file
# - No need to check which system we are on. The init info are comments and
# do not interfere with update_rc.d systems. Additionally:
# Ubuntu/debian by default does not come with chkconfig, but does issue a
# warning if init info is not available.
cat
>
${
TMP_FILE
}
<<
EOT
#/bin/sh
#Configurations injected by install_server below....
EXEC=
$REDIS_EXECUTABLE
CLIEXEC=
$CLI_EXEC
PIDFILE=
$PIDFILE
CONF="
$REDIS_CONFIG_FILE
"
REDISPORT="
$REDIS_PORT
"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_
${
REDIS_PORT
}
is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_
${
REDIS_PORT
}
# 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_
${
REDIS_PORT
}
# Description: Redis daemon
### END INIT INFO
EOT
cat
${
INIT_TPL_FILE
}
>>
${
TMP_FILE
}
#copy to /etc/init.d
cp
-f
$TMP_FILE
$INIT_SCRIPT_DEST
&&
chmod
+x
$INIT_SCRIPT_DEST
||
die
"Could not copy redis init script to
$INIT_SCRIPT_DEST
"
cp
$TMP_FILE
$INIT_SCRIPT_DEST
&&
\
chmod
+x
$INIT_SCRIPT_DEST
||
die
"Could not copy redis init script to
$INIT_SCRIPT_DEST
"
echo
"Copied
$TMP_FILE
=>
$INIT_SCRIPT_DEST
"
#Install the service
echo
"Installing service..."
if
[
!
`
which chkconfig
`
]
;
then
if
command
-v
chkconfig
>
/dev/null 2>&1
;
then
# we're chkconfig, so lets add to chkconfig and put in runlevel 345
chkconfig
--add
redis_
${
REDIS_PORT
}
&&
echo
"Successfully added to chkconfig!"
chkconfig
--level
345 redis_
${
REDIS_PORT
}
on
&&
echo
"Successfully added to runlevels 345!"
elif
command
-v
update-rc.d
>
/dev/null 2>&1
;
then
#if we're not a chkconfig box assume we're able to use update-rc.d
update-rc.d redis_
$REDIS_PORT
defaults
&&
echo
"Success!"
update-rc.d redis_
$
{
REDIS_PORT
}
defaults
&&
echo
"Success!"
else
# we're chkconfig, so lets add to chkconfig and put in runlevel 345
chkconfig
--add
redis_
$REDIS_PORT
&&
echo
"Successfully added to chkconfig!"
chkconfig
--level
345 redis_
$REDIS_PORT
on
&&
echo
"Successfully added to runlevels 345!"
echo
"No supported init tool found."
fi
/etc/init.d/redis_
$REDIS_PORT
start
||
die
"Failed starting service..."
...
...
@@ -187,4 +243,3 @@ fi
#tada
echo
"Installation successful!"
exit
0
utils/redis_init_script.tpl
View file @
68a3af5f
...
...
@@ -25,7 +25,19 @@ case "$1" in
echo "Redis stopped"
fi
;;
status)
if [ ! -f $PIDFILE ]
then
echo 'Redis is not running'
else
echo "Redis is running ($(
<
$
PIDFILE
))"
fi
;;
restart
)
$0
stop
$0
start
;;
*
)
echo "Please use start or st
op
as first argument"
echo
"
Please
use
start
,
stop
,
restart
or
st
atus
as
first
argument
"
;;
esac
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment