shell脚本【生成Java服务的当前运行dump日志】

#!/bin/sh

clear
echo "########################################"
echo " 收集java进程诊断信息"
echo "########################################"

echo "请输入jar包名称以获得pid (ps -ef | grep YourInput):"

read jar_name

ps_line=`ps -ef | grep $jar_name | grep -v grep`
pid=`echo $ps_line | awk '{print $2}'`

echo "-----------------------------"
echo "pid: $pid"
echo "ps命令输出(如果未找到唯一行,请退出并重新出入jar包名称):"
echo "$ps_line"
echo "-----------------------------"

echo "是否对当前进程收集诊断信息?(y/n):"

read choice

if [ $choice != 'y' -a $choice != 'Y' ]; then
  echo "退出,请重新执行。"
  exit 0
fi

dir_name="jvm_trace_${jar_name}_`date '+%Y%m%d_%H_%M_%S'`"


echo "创建文件夹:$dir_name"
mkdir $dir_name

echo "打印top信息…………"
top -H -p $pid -b -n 1 > $dir_name/top.log

echo "打印GC信息…………"
jstat -gccause $pid > $dir_name/gc.log
echo "" >> $dir_name/gc.log
echo "----------------------------------------------------" >> $dir_name/gc.log
echo "" >> $dir_name/gc.log
jstat -gc $pid >> $dir_name/gc.log

echo "打印线程栈信息(时间较长,请稍候)…………"
jstack -l $pid > $dir_name/jstack.log

echo "收集堆内存信息(时间很长,请稍候)…………"
jmap -dump:format=b,file=$dir_name/dump_heap.hprof $pid

echo "已完成收集,请查看${dir_name}目录"

你可能感兴趣的:(java,linux,服务器)