首页 > 代码库 > tomcat启动和jvm性能分析脚本

tomcat启动和jvm性能分析脚本

#!/bin/bash 

 

# chkconfig: - 95 15  

# description: Tomcat start/stop/status script 

#Location of JAVA_HOME (bin files) 

export JAVA_HOME= /usr/java/jdk1.7.0_80

#Add Java binary files to PATH 

export PATH=$JAVA_HOME/bin:$PATH 

#CATALINA_HOME is the location of the configuration files of this instance of Tomcat 

CATALINA_HOME=/usr/local/tomcat 

#TOMCAT_USER is the default user of tomcat 

TOMCAT_USER=www 

 

#TOMCAT_USAGE is the message if this script is called without any options 

TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}" 

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop 

SHUTDOWN_WAIT=20 

tomcat_pid() { 

        echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s " "|cut -d" " -f2` 

start() { 

  pid=$(tomcat_pid) 

  if [ -n "$pid" ];then 

    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m" 

  else 

    echo -e "\e[00;32mStarting tomcat\e[00m" 

    if [ `user_exists $TOMCAT_USER` = "1" ];then 

      su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh 

    else 

      $CATALINA_HOME/bin/startup.sh 

    fi 

    status 

  fi 

  return 0 

 

status(){ 

  pid=$(tomcat_pid) 

  if [ -n "$pid" ];then 

    echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m" 

  else 

    echo -e "\e[00;31mTomcat is not running\e[00m" 

  fi 

stop() { 

  pid=$(tomcat_pid) 

  if [ -n "$pid" ];then 

    echo -e "\e[00;31mStoping Tomcat\e[00m" 

        $CATALINA_HOME/bin/shutdown.sh 

 

    let kwait=$SHUTDOWN_WAIT 

    count=0; 

    until [ `ps -p $pid | grep -c $pid` = ‘0‘ ] || [ $count -gt $kwait ] 

    do 

      echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n"; 

      sleep 1 

      let count=$count+1; 

    done 

    if [ $count -gt $kwait ];then 

      echo -n -e "\n\e[00;31mkilling processes which didn‘t stop after $SHUTDOWN_WAIT seconds\e[00m" 

      kill -9 $pid 

    fi 

  else 

    echo -e "\e[00;31mTomcat is not running\e[00m" 

  fi 

 

  return 0 

 

user_exists(){ 

  if id -u $1 >/dev/null 2>&1; then 

    echo "1" 

  else 

    echo "0" 

  fi 

case $1 in 

        start) 

          start 

        ;; 

        stop)   

          stop 

        ;; 

        restart) 

          stop 

          start 

        ;; 

        status) 

      status 

        ;; 

        *) 

      echo -e $TOMCAT_USAGE 

        ;; 

esac     

exit 0  

 

---------------------------------------------------------------

jvm分析脚本

----------------------

#!/bin/sh

 

DUMP_PIDS=`ps --no-heading -C java -f --width 1000 |awk ‘{print $2}‘`
if [ -z "$DUMP_PIDS" ]; then
echo "The server $HOST_NAME is not started!"
exit 1;
fi

DUMP_ROOT=~/dump
if [ ! -d $DUMP_ROOT ]; then
mkdir $DUMP_ROOT
fi

DUMP_DATE=`date +%Y%m%d%H%M%S`
DUMP_DIR=$DUMP_ROOT/dump-$DUMP_DATE
if [ ! -d $DUMP_DIR ]; then
mkdir $DUMP_DIR
fi

for PID in $DUMP_PIDS ; do
#Full thread dump 用来查线程占用,死锁等问题
$JAVA_HOME/bin/jstack $PID > $DUMP_DIR/jstack-$PID.dump 2>&1
echo -e ".\c"
#打印出一个给定的Java进程、Java core文件或远程Debug服务器的Java配置信息,具体包括Java系统属性和JVM命令行参数。
$JAVA_HOME/bin/jinfo $PID > $DUMP_DIR/jinfo-$PID.dump 2>&1
echo -e ".\c"
#jstat能够动态打印jvm(Java Virtual Machine Statistics Monitoring Tool)的相关统计信息。如young gc执行的次数、full gc执行的次数,各个内存分区的空间大小和可使用量等信息。
$JAVA_HOME/bin/jstat -gcutil $PID > $DUMP_DIR/jstat-gcutil-$PID.dump 2>&1
echo -e ".\c"
$JAVA_HOME/bin/jstat -gccapacity $PID > $DUMP_DIR/jstat-gccapacity-$PID.dump 2>&1
echo -e ".\c"
#未指定选项时,jmap打印共享对象的映射。对每个目标VM加载的共享对象,其起始地址、映射大小及共享对象文件的完整路径将被打印出来,
$JAVA_HOME/bin/jmap $PID > $DUMP_DIR/jmap-$PID.dump 2>&1
echo -e ".\c"
#-heap打印堆情况的概要信息,包括堆配置,各堆空间的容量、已使用和空闲情况
$JAVA_HOME/bin/jmap -heap $PID > $DUMP_DIR/jmap-heap-$PID.dump 2>&1
echo -e ".\c"
#-dump将jvm的堆中内存信息输出到一个文件中,然后可以通过eclipse memory analyzer进行分析
#注意:这个jmap使用的时候jvm是处在假死状态的,只能在服务瘫痪的时候为了解决问题来使用,否则会造成服务中断。
$JAVA_HOME/bin/jmap -dump:format=b,file=$DUMP_DIR/jmap-dump-$PID.dump $PID 2>&1
echo -e ".\c"
#显示被进程打开的文件信息
if [ -r /usr/sbin/lsof ]; then
/usr/sbin/lsof -p $PID > $DUMP_DIR/lsof-$PID.dump
echo -e ".\c"
fi
done
#主要负责收集、汇报与存储系统运行信息的。
if [ -r /usr/bin/sar ]; then
/usr/bin/sar > $DUMP_DIR/sar.dump
echo -e ".\c"
fi
#主要负责收集、汇报与存储系统运行信息的。
if [ -r /usr/bin/uptime ]; then
/usr/bin/uptime > $DUMP_DIR/uptime.dump
echo -e ".\c"
fi
#内存查看
if [ -r /usr/bin/free ]; then
/usr/bin/free -t > $DUMP_DIR/free.dump
echo -e ".\c"
fi
#可以得到关于进程、内存、内存分页、堵塞IO、traps及CPU活动的信息。
if [ -r /usr/bin/vmstat ]; then
/usr/bin/vmstat > $DUMP_DIR/vmstat.dump
echo -e ".\c"
fi
#报告与CPU相关的一些统计信息
if [ -r /usr/bin/mpstat ]; then
/usr/bin/mpstat > $DUMP_DIR/mpstat.dump
echo -e ".\c"
fi
#报告与IO相关的一些统计信息
if [ -r /usr/bin/iostat ]; then
/usr/bin/iostat > $DUMP_DIR/iostat.dump
echo -e ".\c"
fi
#报告与网络相关的一些统计信息
if [ -r /bin/netstat ]; then
/bin/netstat > $DUMP_DIR/netstat.dump
echo -e ".\c"
fi
echo "OK!"

 

tomcat启动和jvm性能分析脚本