把redis配置为开机启动有很多方法
但看过官方的脚本,还是用官方的(自己shell水平还是还自知之明的…)
1.复制脚本到系统目录中
cp /redis/utils/redis_init_script /etc/rc.d/init.d/redis
2.修改脚本 vim /etc/rc.d/init.d/redis
#!/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
1).在#!/bin/sh
添加#chkconfig: 2345 80 90
,(如果不添加,注册服务时:service redis does not support chkconfig)
2).REDISPORT根据自己环境配置来配置
3).EXEC=/usr/local/bin/redis-server 改为自己环境下EXEC=redis/src/redis-server
4).CLIEXEC=/usr/local/bin/redis-cli 改为自己环境下CLIEXEC=redis/src/redis-cli
5).CONF=/etc/redis/{REDISPORT}.conf”改为自己环境下CONF=redis/redis.conf
6).PIDFILE=/var/run/redis_{REDISPORT}.pid改为自己环境下的配置(redis.conf里的:pidfile /var/run/redis_6379.pid)
7).在$EXEC $CONF
后面加上& $EXEC $CONF &
修改后的文件
#!/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=6377
EXEC=/home/software/redis/src/redis-server
CLIEXEC=/home/software/redis/src/redis-cli
PIDFILE=/home/data/redis/run/redis.pid
CONF="/home/software/redis/redis.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
3.注册服务: chkconfig --add redis
[4].防火墙配置
添加配置:echo '-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT' >> /etc/sysconfig/iptables
重启:service iptables restart
[5].启动服务 service redis start
[6].测试 reboot
ps -ef | grep redis