02.利用Shell开发Redis多实例启动脚本

SciRui 2020-02-21

一、Redis部署情况

## 软件部署情况
[ ~]# ls -ld /data/apps/redis   <==目录本身及以下所有文件的属主/组为redis
lrwxrwxrwx 1 root root 24 Oct 26 11:33 /data/apps/redis -> /data/apps/redis-4.0.14/

## 实例部署情况
[ ~]# tree /data/redis/         <==目录本身及以下所有文件的属主/组为redis
/data/redis/
├── 6379
    ├── conf  # 6379实例的配置文件
    ├── data  # 6379实例的数据目录(不管使用不使用持久化,都给创建好)
    ├── logs  # 6379实例的日志文件
    └── run   # 6379实例pid的存放路径

二、Redis启停方式

## Redis服务用什么用户来启动
01:Redis建议使用普通用户来启动,不要用root用户来启动;
02:我一般会在操作系统下创建一个redis普通用户(要有家目录,要有密码,用户和密码不能过期)

## Redis的启停方式
启动:redis-server /data/redis/6379/conf/redis.conf
停止:redis-cli -h "可连接到redis实例的IP" -p "端口" -a "密码" shutdown >/dev/null 2>&1

三、Redis启动和停止的脚本内容

注意:该脚本只有root用户能够执行(脚本中进行限制了的),在启动redis服务时,是通过su - redis -c "redis的启动方式"来进行启动的

#!/bin/bash
#
# Define variables
RETVAL=0
User="redis"
Port=6379
Pass="chenliang"
Bind="172.16.1.31"
Exec="/data/apps/redis/src/redis-server"
Cliexec="/data/apps/redis/src/redis-cli"
Conf="/data/redis/6379/conf/redis.conf"
Pid="/data/redis/6379/run/redis.pid"
 
# Determine ther user to execute
if [ "$UID" -ne "$RETVAL" ];then
   echo "Must be root to run scripts"
   exit 1
fi
 
# Load functions
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
 
# Define functions
start(){
    if [ ! -f $Pid ];then
       /usr/bin/su - $User -c "$Exec $Conf"
       RETVAL=$?
       if [ $RETVAL -eq 0 ];then
          action "Start redis[$Port] service" /bin/true
         else
          action "Start redis[$Port] service" /bin/false
       fi
     else
       echo "redis[$Port] service is running"
    fi
    return $RETVAL
}
 
stop(){
    if [ -f $Pid ];then
       $Cliexec -h $Bind -p $Port -a $Pass shutdown >/dev/null 2>&1
       RETVAL=$?
       if [ $RETVAL -eq 0 ];then
          action "Stop redis[$Port] service" /bin/true
         else
          action "Stop redis[$Port] service" /bin/false
       fi
      else
       echo "redis[$Port] is not running"
    fi
    return $RETVAL
}
 
status(){
    if [ -f $Pid ];then
       echo "redis[$Port] service is running"
      else
       echo "redis[$Port] service is not running"
    fi
    return $RETVAL
}
 
# Case call functions
case "$1" in
    start)
        start
        RETVAL=$?
        ;;
    stop)
        stop
        RETVAL=$?
        ;;
    restart)
        stop
        sleep 2
        start
        RETVAL=$?
        ;;
    status)
        status
        RETVAL=$?
        ;; 
    *)
        echo "USAGE:$0{start|stop|restart|status}"
        exit 1
esac

四、脚本执行结果

# 查看redisd脚本有哪些选项
[ ~]# /data/redis/6379/redisd 
USAGE:/data/redis/6379/redisd{start|stop|restart|status}

# 查看当前redis 6379实例的状态
[ ~]# /data/redis/6379/redisd status
redis[6379] service is not running

# 启动redis 6379实例并查看状态
[ ~]# /data/redis/6379/redisd start
Start redis[6379] service                                  [  OK  ]
[ ~]# lsof -i :6379
COMMAND    PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 8000 redis    6u  IPv4  34575      0t0  TCP node31:6379 (LISTEN)

# 重启redis 6379实例并查看状态
[ ~]# /data/redis/6379/redisd restart
Stop redis[6379] service                                   [  OK  ]
Start redis[6379] service                                  [  OK  ]
[ ~]# 
[ ~]# lsof -i :6379
COMMAND    PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 8042 redis    6u  IPv4  34747      0t0  TCP node31:6379 (LISTEN)

# 停止redis 6379实例
[ ~]# /data/redis/6379/redisd stop
Stop redis[6379] service                                   [  OK  ]
[ ~]# lsof -i :6379
[ ~]#

五、加入开机自启动

我一般是放在/etc/rc.local文件,并让该文件的权限为744

[ ~]# echo -ne "\n# Boot start redis service[6379]. USER:chenliang TIME:$(date +%F)\n/data/redis/6379/redisd start\n" >>/etc/rc.local
[ ~]# tail -2 /etc/rc.local 
# Boot start redis service[6379]. USER:chenliang TIME:2020-02-17
/data/redis/6379/redisd start

相关推荐

DiamondTao / 0评论 2020-08-30