Redis开机自启动

时间:2021-03-29 15:38:39

1.新建redis文件

   vi /etc/init.d/redis

2.修改/tec/init.d/redis文件,要修改Redis安装目录及配置文件路径

EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
CONF="/usr/local/redis/bin/redis.conf"

 

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

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

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/bin/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

 #!/bin/sh

   # chkconfig: 2345 10 90

   # description: Start and Stop redis

   头部的这部份要包含,否则chkconfig命令会执行不成功

3.开机启动设置

  3.1添加Redis服务

  chkconfig --add redis

   3.2设备开机启动

  chkconfig redis on

    3.3启动Redis

  service redis start

     3.4关闭Redis

  service redis stop

 4.重新启动系统后查看进程

   ps aux|grep redis

   Redis开机自启动