最近碰到reids客户端连接数达到1000,超过reids.confi里的

# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
## maxclients 10000
查看reids-cli里的client list 发现很多连接,使用client kill命令结束。因为数量多,要一个一个Kill比较麻烦
所以需要些个脚本批处理,后面附上脚本代码
主要原因是本地测试项目时候直接关闭后,redis 连接没有主动关闭。有这几个redis配置可以主动关闭
空闲的连接
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.tcp-keepalive 300
这个主动连接客户端,但耗性能。
后面附上结束redis客户端脚本
1 #/bin/bash
  2 
  3 function read_line()
  4 {
  5  (redis-cli -h 127.0.0.1 -p 6379 -a leapmotor10214.pwd client list) >> /tmp/redis.txt
  6  cat /tmp/redis.txt | while read line
  7  do
  8   linestr="$line"
  9   #idbg=$[$lidbg+3]
 10   lidbg1=$(expr index "$linestr" r)
 11   lidbg=$[$lidbg1 + 2]
 12   lidend=$(expr index "$linestr" f)
 13   lidend=$[$lidend-$lidbg]
 14   port1=`expr substr "$linestr" $lidbg $lidend`
 15   redis-cli -h 127.0.0.1 -p 6379 -a leapmotor10214.pwd client kill $port1
 16   echo $port2
 17  done
 18 }
 19 
 20 read_line