1.将redis服务脚本文件拷贝至/etc/rc.d/init.d目录下;
cp /usr/local/redis-3.2.3/utils/redis_init_script /etc/rc.d/init.d/redis
2.修改redis服务脚本,vi/etc/rc.d/init.d/redis;修改完的/etc/rc.d/redis如下:
#!/bin/sh
#
# chkconfig: 2345 80 90
# 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
2.1.在文档 #!/bin/sh添加# chkconfig: 2345 80 90
2.2.将REDISPORT=6379改成需要的端口号;
2.3.注意到EXEC=/usr/local/bin/redis-server,假如/usr/local/bin下没有对应的redis-server、redis-cli等可执行文件,则需要将其拷贝到/usr/local/bin/目录下;
2.4.注意到CONF="/etc/Redis/${REDISPORT}.conf"。将redis配置文件拷贝到/etc/redis/下,命令如下:
mkdir /etc/redis
cp /usr/local/redis-3.2.3/redis.conf /etc/redis/6379.conf
2.5.在start方法中的$EXEC $CONF 后边加一个&,表示让redis作为后台服务启动,否则该服务会独占输入屏。
3.将/etc/rc.d/init.d/redis脚本添加到服务,命令如下:
chkconfig --add redis
4.启动redis:service redis start
停止redis: service redis stop