shell 分析java进程cpu使用率过高的shell脚本
发布时间:2020-12-15 07:07:15  所属栏目:安全  来源:网络整理 
            导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/bin/bash# @Function# Find out the highest cpu consumed threads of java,and print the stack of these threads.## @Usage# $ ./show-busy-java
                
                
                
            | 以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #!/bin/bash
# @Function
# Find out the highest cpu consumed threads of java,and print the stack of these threads.
#
# @Usage
#   $ ./show-busy-java-threads.sh
#
# @author Jerry Lee
PROG=`basename $0`
usage() {
    cat <<EOF
Usage: ${PROG} [OPTION]...
Find out the highest cpu consumed threads of java,and print the stack of these threads.
Example: ${PROG} -c 10
Options:
    -p,--pid       find out the highest cpu consumed threads from the specifed java process,default from all java process.
    -c,--count     set the thread count to show,default is 5
    -h,--help      display this help and exit
EOF
    exit $1
}
ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "[email?protected]"`
[ $? -ne 0 ] && usage 1
eval set -- "${ARGS}"
while true; do
    case "$1" in
    -c|--count)
        count="$2"
        shift 2
        ;;
    -p|--pid)
        pid="$2"
        shift 2
        ;;
    -h|--help)
        usage
        ;;
    --)
        shift
        break
        ;;
    esac
done
count=${count:-5}
redEcho() {
    [ -c /dev/stdout ] && {
        # if stdout is console,turn on color output.
        echo -ne " 33[1;31m"
        echo -n "[email?protected]"
        echo -e " 33[0m"
    } || echo "[email?protected]"
}
# Check the existence of jstack command!
if ! which jstack &> /dev/null; then
    [ -z "$JAVA_HOME" ] && {
        redEcho "Error: jstack not found on PATH!"
        exit 1
    }
    ! [ -f "$JAVA_HOME/bin/jstack" ] && {
        redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack file does NOT exists!"
        exit 1
    }
    ! [ -x "$JAVA_HOME/bin/jstack" ] && {
        redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack is NOT executalbe!"
        exit 1
    }
    export PATH="$JAVA_HOME/bin:$PATH"
fi
uuid=`date +%s`_${RANDOM}_$$
cleanupWhenExit() {
    rm /tmp/${uuid}_* &> /dev/null
}
trap "cleanupWhenExit" EXIT
printStackOfThread() {
    while read threadLine ; do
        pid=`echo ${threadLine} | awk '{print $1}'`
        threadId=`echo ${threadLine} | awk '{print $2}'`
        threadId0x=`printf %x ${threadId}`
        user=`echo ${threadLine} | awk '{print $3}'`
        pcpu=`echo ${threadLine} | awk '{print $5}'`
        jstackFile=/tmp/${uuid}_${pid}
        [ ! -f "${jstackFile}" ] && {
            jstack ${pid} > ${jstackFile} || {
                redEcho "Fail to jstack java process ${pid}!"
                rm ${jstackFile}
                continue
            }
        }
        redEcho "Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"
        sed "/nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}
    done
}
ps -Leo pid,lwp,user,comm,pcpu --no-headers | {
    [ -z "${pid}" ] &&
    awk '$4=="java"{print $0}' ||
    awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}'
} | sort -k5 -r -n | head --lines "${count}" | printStackOfThread以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
推荐文章
            站长推荐
            - Angularjs ui bootstrap:如何垂直中心模态组件?
- ssh user@host "command" 与login host运
- bootstrap笔记备忘
- 变量 – 在angularjs中的src属性中使用ng-repeat
- CXF发布restful WebService的入门例子(服务器端)
- Sublime Text2插件安装(win)
- bash – 如果该字段前面有$符号,我如何检查某个字
- 利用Axis2开发WebService(4)---用wsdl2java简化客
- 读书笔记 <Bootstrap Yourself with Linux USB S
- Bootstrap3 按钮-激活状态
热点阅读
            