将redis服务(redis、sentinel)做成service并开机启动

此文章是文章reids双机热备的后续

将redis服务做成service并开机启动

将redis做成redis-server,将sentinel做成redis-sentinel服务

redis-server服务

将redis.conf文件复制到/etc/redis下并且重命名为6379.conf

mkdir -p /etc/redis
cp ./redis.conf /etc/redis/6379.conf

./utils/redis_init_script复制到/etc/init.d目录下,并重命名为redis-server

cp ./utils/redis_init_script /etc/init.d/redis-server

修改redis-server文件,内容如下

#!/bin/sh
## 这两行是加的
# chkconfig: 2345 10 90  
# description: Start and Stop redis 

#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
## 将对应的地址填入即可
EXEC=/home/tmp/redis-test/redis-4.0.9/src/redis-server
CLIEXEC=/home/tmp/redis-test/redis-4.0.9/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
AUTH="123456"
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 $AUTH -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

然后为其赋予权限即可通过service的方法进行服务的启动和停用。

打开redis命令:service redis-server start

关闭redis命令:service redis-server stop

设为开机启动:chkconfig redis-server on

设为开机关闭:chkconfig redis-server off

redis-sentinel服务

将sentinel.conf文件复制到/etc/redis下并且重命名为26379.conf

mkdir -p /etc/redis
cp ./sentinel.conf /etc/redis/26379.conf

./utils/redis_init_script复制到/etc/init.d目录下,并重命名为redis-sentinel

cp ./utils/redis_init_script /etc/init.d/redis-sentinel

修改redis-sentinel文件,内容如下

#!/bin/sh
## 这两行是加的
# chkconfig: 2345 10 90  
# description: Start and Stop redis 

#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=26379
## 将对应的地址填入即可
EXEC=/home/tmp/redis-test/redis-4.0.9/src/redis-sentinel
CLIEXEC=/home/tmp/redis-test/redis-4.0.9/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
AUTH="123456"

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 $AUTH -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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值