把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=6379EXEC=/usr/local/bin/redis-serverCLIEXEC=/usr/local/bin/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pidCONF="/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}.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=6377EXEC=/home/software/redis/src/redis-serverCLIEXEC=/home/software/redis/src/redis-cliPIDFILE=/home/data/redis/run/redis.pidCONF="/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