Linux下Redis開機自啟(Centos)

silent發表於2014-12-11

廢話少說,直接來步驟:

1、設定redis.conf中daemonize為yes,確保守護程式開啟。

2、編寫開機自啟動指令碼

vi /etc/init.d/redis

指令碼內容如下:

# chkconfig: 2345 10 90  
# description: Start and Stop redis   
  
PATH=/usr/local/bin:/sbin:/usr/bin:/bin   
REDISPORT=6379  
EXEC=/usr/redisbin/redis-server   
REDIS_CLI=/usr/redisbin/redis-cli   
 
PIDFILE=/var/run/redis.pid   
CONF="/usr/redisbin/redis.conf"  
AUTH="1234"  

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   
                if [ "$?"="0" ]   
                then   
                        echo "Redis is running..."  
                fi   
                ;;   
        stop)   
                if [ ! -f $PIDFILE ]   
                then   
                        echo "$PIDFILE exists, process is not running."  
                else  
                        PID=$(cat $PIDFILE)   
                        echo "Stopping..."  
                       $REDIS_CLI -p $REDISPORT  SHUTDOWN    
                        sleep 2  
                       while [ -x $PIDFILE ]   
                       do  
                                echo "Waiting for Redis to shutdown..."  
                               sleep 1  
                        done   
                        echo "Redis stopped"  
                fi   
                ;;   
        restart|force-reload)   
                ${0} stop   
                ${0} start   
                ;;   
        *)   
               echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2  
                exit 1  
esac

3、寫完後儲存退出VI

4、設定許可權

chmod 755 redis

5、啟動測試

/etc/init.d/redis start

啟動成功會提示如下資訊:

Starting Redis server...
Redis is running...

使用redis-cli測試:

[root@rk ~]# /usr/redisbin/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> exit

6、設定開機自啟動

chkconfig redis on

7、關機重啟測試

reboot

然後在用redis-cli測試即可。

 

相關文章