CentOS设置redis自启动脚本

一:编写shell脚本

vi /etc/init.d/redis

copy下面内容(注意:以自己实际的文件路径修改

#!/bin/bash
#chkconfig: 22345 10 90
#description: Start and Stop redis

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

PIDFILE=/var/run/redis.pid
CONF="/etc/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

二:执行以下命令

chmod +x /etc/init.d/redis  #给脚本设置权限
chkconfig --add redis  #添加nginx服务
chkconfig redis on  #设置nginx服务开机启动
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页