系统环境:
$ cat /etc/issue
Red Hat Enterprise Linux Server release 5.8 (Tikanga)
Kernel \r on an \m
官方下载地址:Redis.io/download
下载最新稳定版 redis-3.0.4.tar.gz,大小仅 1.3 MB。
将下载的安装包放在用户目录下,如 /home/webapp,打算将其安装到 /opt/redis:
$ sudo mkdir /opt/redis
$ cd /home/webapp
$ tar xzf redis-3.0.4.tar.gz
$ cd redis-3.0.4
$ sudo make PREFIX=/opt/redis install
之后查看 /opt/redis,下边只生成了一个目录,该目录下只有六个文件:
redis-benchmark redis-check-dump redis-sentinel
redis-check-aof redis-cli redis-server
安装完成。
查看安装包自带服务脚本:
$ cat /home/webapp/redis-3.0.4/utils/redis_init_script
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
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
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
将其拷贝到 /etc/rc.d/init.d 目录并重命名为 redis:
$ sudo cp /home/webapp/redis-3.0.4/utils/redis_init_script /etc/rc.d/init.d/redis
然后依据 redis 的安装路径修改 /etc/rc.d/init.d/redis 为:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/opt/redis/bin/redis-server
CLIEXEC=/opt/redis/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/redis/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
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
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
注意红色字体部分:
chkconfig、description 两行注释必须,缺一不可,否则 service redis does not support chkconfig;
$EXEC $CONF &,后边的 &,表示将服务转到后台运行;
EXEC、CLIEXEC、CONF 等三处路径都要改。
2.2 复制配置文件