打印jvm堆栈信息脚本(这个是docker容器便捷执行脚本)

注意点:docker容器里面依赖的需要是JDK而不是JRE,JRE没有jstack工具

#!/bin/bash
if [ $# -le 0 ]; then
	echo "usage: $0 <pid> [line-number]"
	exit 1
fi

# java home
if test -z $JAVA_HOME 
then
    JAVA_HOME='/usr/bin'
fi

# pid
pid=$1
echo "input pid is $pid, then use docker java pid"

# line number
linenum=$2
if test -z $linenum
then
    linenum=15
fi

# docker java pid
pid=$(top -bn1 | grep java | awk '{print $1}')
echo "docker java pid is $pid"

# checking pid
if test -z "$($JAVA_HOME/bin/jps -l | cut -d '' -f 1 | grep $pid)"
then
    echo "process of $pid is not exists"
fi

# file position
threadsfile=$PWD/threads$pid.dump
echo "threadsfile position is $threadsfile"
stackfile=$PWD/stack$pid.dump
echo "stackfile position is $stackfile"
threadstackfile=$PWD/threadstackfile$pid.dump
echo "threadstackfile position is $threadstackfile"

# generate java stack
$JAVA_HOME/bin/jstack -l $pid >> $stackfile

# generate process's thread file
ps -mp $pid -o THREAD,tid,time | sort -k2r | awk '{if ($1 !="USER" && $2 != "0.0" && $8 !="-") print $8;}' | xargs printf "%x\n" >> $threadsfile

# generate thread stack file
tids="$(cat $threadsfile)"
for tid in $tids
do
	echo "------------------------------ ThreadId ($tid) ------------------------------"
	cat $stackfile | grep 0x$tid -A $linenum
    echo "------------------------------ ThreadId ($tid) ------------------------------" >> $threadstackfile
    cat $stackfile | grep 0x$tid -A $linenum >> $threadstackfile
done

# rm -f $stackfile $threadsfile
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值