CentOS服务器中设置Redis开机自启

2022年10月7日11:15:37
  1. Linux环境下,将Redis添加到service,使用脚本启动和停止,并且可以设置开机自启动
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
#设置密码
REDISPWD=123456
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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 -a $REDISPWD -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
  1. 保存上面脚本至 /etc/init.d/redis,设置开机自启
[root@harbor ~]# cp redis /etc/init.d/redis
[root@harbor ~]# chkconfig --add redis
[root@harbor ~]# chkconfig redis on
  • 作者:DMcomming
  • 原文链接:https://blog.csdn.net/DMcomming/article/details/93739215
    更新时间:2022年10月7日11:15:37 ,共 869 字。