这个办法在大部分情况下都可以找到占用 Cache 较多的程序和进程。脚本内容如下:
- #!/bin/bash
- #Author: Shanker
- #Time: 2016/06/08
- #set -e
- #set -u
- #you have to install linux-fincore
- if [ ! -f /usr/local/bin/linux-fincore ]
- then
- echo "You haven't installed linux-fincore yet"
- exit
- fi
- #find the top 10 processs' cache file
- ps -e -o pid,rss|sort -nk2 -r|head -10 |awk '{print $1}'>/tmp/cache.pids
- #find all the processs' cache file
- #ps -e -o pid>/tmp/cache.pids
- if [ -f /tmp/cache.files ]
- then
- echo "the cache.files is exist, removing now "
- rm -f /tmp/cache.files
- fi
- while read line
- do
- lsof -p $line 2>/dev/null|awk '{print $9}' >>/tmp/cache.files
- done</tmp/cache.pids
- if [ -f /tmp/cache.fincore ]
- then
- echo "the cache.fincore is exist, removing now"
- rm -f /tmp/cache.fincore
- fi
- for i in `cat /tmp/cache.files`
- do
- if [ -f $i ]
- then
- echo $i >>/tmp/cache.fincore
- fi
- done
- linux-fincore -s `cat /tmp/cache.fincore`
- rm -f /tmp/cache.{pids,files,fincore}
比较遗憾的是,linux-ftools 目前已经不再维护了。在新版本的操作系统上没法编译好这个程序,所以这个方法失效了。
再次通过万能的 Google 搜索,后来我找到了 pcstat 这个工具,pcstat 使用 Go 语言开发,功能基本和 linux-ftools 一样 。
项目地址:https://github.com/tobert/pcstat
然后我修改了 Shanker 的脚本,让它使用 pcstat 来进行处理,这样就可以很好的找到 Cache 所占用的情况。修改后的脚本如下:
- #!/bin/bash
- #you have to install pcstat
- if [ ! -f /data0/brokerproxy/pcstat ]
- then
- echo "You haven't installed pcstat yet"
- echo "run \"go get github.com/tobert/pcstat\" to install"
- exit
- fi
- #find the top 10 processs' cache file
- ps -e -o pid,rss|sort -nk2 -r|head -10 |awk '{print $1}'>/tmp/cache.pids
- #find all the processs' cache file
- #ps -e -o pid>/tmp/cache.pids
- if [ -f /tmp/cache.files ]
- then
- echo "the cache.files is exist, removing now "
- rm -f /tmp/cache.files
- fi
- while read line
- do
- lsof -p $line 2>/dev/null|awk '{print $9}' >>/tmp/cache.files
- done</tmp/cache.pids
- if [ -f /tmp/cache.pcstat ]
- then
- echo "the cache.pcstat is exist, removing now"
- rm -f /tmp/cache.pcstat
- fi
- for i in `cat /tmp/cache.files`
- do
- if [ -f $i ]
- then
- echo $i >>/tmp/cache.pcstat
- fi
- done
- /data0/brokerproxy/pcstat `cat /tmp/cache.pcstat`
- rm -f /tmp/cache.{pids,files,pcstat}
(编辑:ASP站长网)
|