ubuntu redis集群服务自启

redis集群搭建

一、自启脚本

redis_6379服务启动脚本

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

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
PASSWORD=sa
EXEC=/opt/redis-cluster/6379/bin/redis-server
CLIEXEC=/opt/redis-cluster/6379/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/redis-cluster/6379/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 -a $PASSWORD -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
	status)
        PID=$(cat $PIDFILE)
		if [ ! -x /proc/${PID} ]
        then
                echo 'Redis is not running'
        else
                echo "Redis is running ($PID)"
        fi
        ;;
	status)
        $0 stop
		$0 start
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

redis_6380服务启动脚本

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

### BEGIN INIT INFO
# Provides:     redis_6380
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6380
PASSWORD=sa
EXEC=/opt/redis-cluster/6380/bin/redis-server
CLIEXEC=/opt/redis-cluster/6380/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/redis-cluster/6380/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 -a $PASSWORD -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
	status)
        PID=$(cat $PIDFILE)
		if [ ! -x /proc/${PID} ]
        then
                echo 'Redis is not running'
        else
                echo "Redis is running ($PID)"
        fi
        ;;
	status)
        $0 stop
		$0 start
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

脚本可自行根据下图路径中的redis_init_script脚本修改
在这里插入图片描述
自启脚本拷贝至/etc/init.d目录下

# 移动自启脚本
sudo mv /home/Desktop/redis_6379 /etc/init.d/
sudo mv /home/Desktop/redis_6380 /etc/init.d/

二、redis.conf配置修改

redis配置要设置daemonize为yes 以及配置log文件路径 logfile(记得手动创建日志文件),使服务可以后台运行:

################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "/usr/local/redis-5.0.10/log/redis-server6379.log"

三、执行开机自启设置命令

# 自启脚本添加可执行权限
sudo chmod +x /etc/init.d/redis_6379
sudo chmod +x /etc/init.d/redis_6380
# 更新启动项脚本
sudo update-rc.d redis_6379 defaults
sudo update-rc.d redis_6380 defaults

即可重启测试:

# 重启
reboot

在这里插入图片描述
redis服务自启成功
在这里插入图片描述

四、常用命令

# 启动Redis服务
service redis_6379 start
service redis_6380 start
# 关闭服务
service redis_6379 stop
service redis_6380 stop
# 重启服务
service redis_6379 restart
service redis_6380 restart
# 查看redis状态
service redis_6379 status
service redis_6380 status
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值