shell scripts

1、统计redis 的连接

echo 'client list' | /apps/svr/redis/bin/redis-cli -h 127.0.0.1 -p 6379 |awk '{print $1}' | awk -F '[=,:]' '{print $2}' | sort -n | uniq -c |sort -nr

当redis连接满了,client list都没法执行,那么是用netsta命令查看。

netstat  -na| grep 6379  | awk '{print $5}' | awk -F":" '{print $1}' |  sort  | uniq -c

2、当redis实例cpu较高的时间,捕获redis正在执行的命令

#!/bin/bash

port=$1

rpid=`ps aux | grep redis-server| grep -v grep | grep $port | awk '{print $2}'`

for((;;))
do
        mon=`top -c -b -n 1 -p $rpid | tail -2|grep -Ev "^$"|awk '{if($9 > 15) print 1;else print 0}'`

        if [ $mon = 1 ]
        then     
                /apps/svr/redis/bin/redis-cli -h 127.0.0.1 -p $port monitor > /tmp/$port.txt & 

                echo 'client list'|/apps/svr/redis/bin/redis-cli -h 127.0.0.1 -p $port > /tmp/$port'_client'.txt 

                sleep 1
                ps aux | grep redis | grep -v 'grep' |grep 'redis-cli' | grep 'monitor' | grep $port | awk '{print $2}' | xargs kill  -s 9
                exit 
        fi

        sleep 0.5
done

改进,优雅的方式杀掉子进程:

#!/bin/bash

port=$1
# get the pid of redis-server of specified port
rpid=`ps aux | grep redis-server| grep -v grep | grep $port | awk '{print $2}'`

#when cpu usage is greater than 50%,begin to catch the redis query log by using the redis MONITOR command
mon=`top -c -b -n 1 -p $rpid | tail -2|grep -Ev "^$"|awk '{if($9 > 50) print 1;else print 0}'`

if [ $mon = 1 ]
then
        /apps/svr/redis/bin/redis-cli -h 127.0.0.1 -p $port monitor > /tmp/$port.txt &;

        mpid=$!
        sleep 3
        # killing  the subprocess and exit
        kill $mpid
        exit
fi
此条目发表在os/linux分类目录,贴了标签。将固定链接加入收藏夹。

shell scripts》有 1 条评论

  1. Flora说:

    好久访问了,今天刚把域名续费,继续。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>