redis设置开机启动

谁都会走 提交于 2019-12-03 19:57:16

1.编写脚本

vi /etc/init.d/redis

2.复制下面代码到脚本中(注意要修改里面redis的安装路径,以 /usr/local/redis-4.0.1/路径为例)(这段代码就是redis根目录 /utils/redis_init_script 启动脚本的代码)

#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis

REDISPORT=6379
EXEC=/usr/local/redis-4.0.1/src/redis-server
CLIEXEC=/usr/local/redis-4.0.1/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis-4.0.1/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
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    *)
        echo "Please use start or stop or restart as first argument"
        ;;
esac

3.保存退出,设置权限

chmod 777 /etc/init.d/redis


4.启动redis

打开redis命令:service redis start

关闭redis命令:service redis stop

设为开机启动:chkconfig redis on

设为开机关闭:chkconfig redis off


 



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!