Skip to content

Instantly share code, notes, and snippets.

@oldratlee
Last active June 18, 2019 06:39
Show Gist options
  • Save oldratlee/7413404 to your computer and use it in GitHub Desktop.
Save oldratlee/7413404 to your computer and use it in GitHub Desktop.
对内存使用超过10%进程,执行内存dump和pstack
#!/bin/bash
PROG=`basename $0`
timestamp=`date "+%Y%m%d_%H%M%S"`
TMP=/tmp/${PROG}_${timestamp}
ge() {
[ $(echo "$1 >= $2" | bc) == 1 ]
}
dumpMemBiter() {
thresholdPercent="$1"
while read threadLine ; do
pid=`echo ${threadLine} | awk '{print $1}'`
pmem=`echo ${threadLine} | awk '{print $3}'`
ge "$pmem" "$thresholdPercent" && {
echo dumping $pid
mkdir -p $TMP
cat /proc/${pid}/maps > $TMP/$pid.maps
pstack $pid > $TMP/$pid.pstack
}
done
}
ps -eo pid,user,pmem,comm --no-header | awk '$4=="httpd"{print $0}' | sort -k3,3 -nr | dumpMemBiter 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment