linux设置自启动redis
vi /etc/init.d/redis

# chkconfig: 2345 10 90  
# description: Start and Stop redis   

PATH=/usr/local/bin:/sbin:/usr/bin:/bin   
REDISPORT=6379  
# 自己的redis-server路径(需要自己更改)
EXEC=/home/senya/redis/redis-3.2.6/src/redis-server   
REDIS_CLI=/home/senya/redis/redis-3.2.6/src/redis-cli   

PIDFILE=/var/run/redis.pid
# 自己的redis.conf 路径(需要自己更改)
CONF="/usr/local/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   
                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

  

4、设置权限

chmod 755 redis

5、启动测试

/etc/init.d/redis start

启动成功会提示如下信息:

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

使用redis-cli测试:

复制代码
[root@rk ~]# /home/senya/redis/redis-3.2.6/src/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测试即可。

 

posted on 2018-12-15 22:12  james-roger  阅读(675)  评论(0编辑  收藏  举报