#! /bin/bash
# process-monitor.sh
serverName="/usr/local/apache-tomcat-7.0.72-8080"
#获取进程id
pid=$(ps x | grep $serverName | grep -v grep | awk '{print $1}')
echo $pid
#如果当前服务进程存在
if [ -n "$pid" ];then
mkdir /home/$pid
#dump 方法栈信息
jstack $pid > /home/$pid/jstack.txt
#dump jvm内存使用情况
jmap -heap $pid > /home/$pid/jmapheap.txt
#dump jvm二进制的内存详细使用情况 (set JAVA_OPTS=%JAVA_OPTS% -server -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:heapdump)
jmap -dump:format=b,file=/home/$pid/jmapdump.txt $pid
echo "=====================please input Ctrl + C================="
# dump 某进程的子线程内存和cpu使用情况(top -n1 -Hp $pid 加了-n1则表示只输出一次否则阻塞一直输出直到手动停止)
top -Hp $pid > /home/$pid/top-Hp.txt
echo "=====================tomcat shutdowning================="
$serverName/bin/shutdown.sh
echo "=====================tomcat shutdowned====================="
kill -9 $pid
echo "=====================tomcat killed====================="
fi
$serverName/bin/startup.sh
tail -f $serverName/logs/catalina.out