redis常用bash脚本

  • redis批量启动
#!/bin/bash
echo "----- start sentinel redis servers --------"
cd /usr/local/redis
sudo sudo ./redis-server ./redis.conf
sudo ./redis-server ./redis-6378.conf
sudo ./redis-server ./redis-6380.conf
sudo ./redis-server ./redis-sentinel-26379.conf --sentinel
echo "----- sentinel redis servers start successfully! ------"
  • redis命令参数化连接
    因为配置多个redis实例,每个实例对应不同端口.通过脚本简化redis连接客户端传入的端口参数.
#!/bin/bash
echo "---- connection redis client sudo ./redis-cli -p $1 ------"
cd /usr/local/redis
# 其中$0 得到命令语句 $1代表命令后的第一个参数
sudo ./redis-cli -p $1 -a x@123456

使用时:

## 等价于 redis-cli -p 6379
[centx@centx redis]$ ./con_redis.sh 6379 
  • redis批量关闭
    当启动多个redis实例和sentinel哨兵节点时候,想快速关闭所有redis节点:
#!/bin/bash

pid_name="redis"
echo "--- start kill process' name : " $pid_name

# search process' name contains redis
# $2表示取出第二列的值,对应就是进程号
pid=$(ps -ef|grep $pid_name | grep -v grep | awk '{print $2}')

echo "pid list: " $pid

for item in $pid
do
   echo "kill process pid = " $item
   sudo kill -9 $item
done
~     

使用运行:

[centx@centx redis]$ ps -ef|grep redis
root     19863     1  0 09:39 ?        00:00:04 ./redis-server 127.0.0.1:6379
root     19870     1  0 09:39 ?        00:00:02 ./redis-server 127.0.0.1:6378
root     19876     1  0 09:39 ?        00:00:02 ./redis-server 127.0.0.1:6380
root     19882     1  0 09:39 ?        00:00:05 ./redis-server 127.0.0.1:26379 [sentinel]
centx    21757 20611  0 10:11 pts/6    00:00:00 grep --color=auto redis 

[centx@centx redis]$ ./kill_redis.sh
--- start kill process' name :  redis
pid list:  19863 19870 19876 19882 22013 22014
kill process pid =  19863
kill process pid =  19870
kill process pid =  19876
kill process pid =  19882
kill process pid =  22013
Killed 

推荐阅读更多精彩内容