On-line Java application, in the peak of the Business Times often appear CPU run high, need to see real-time thread CPU situation, the following is a very good script, you can quickly export the CPU of each thread, combined with Jstack log, to the specific thread class name.
First, get the process ID of the JVM:
Copy Code code as follows:
Ps-ef|grep Java
Tomcat 374 372 1 11:45? 00:02:30 Jsvc.exec-java-home/usr/java/latest-user tomcat-pidfile/usr/local/tomcat7/logs/tomcat7.pid-outfile/usr/ Local/tomcat7/logs/catalina-daemon.out-errfile &1-classpath/usr/local/tomcat7/bin/bootstrap.jar:/usr/local/ tomcat7/bin/commons-daemon.jar:/usr/local/tomcat7/bin/tomcat-juli.jar-djava.util.logging.config.file=/usr/ local/tomcat7/conf/logging.properties-djava.awt.headless=true-xmx10240m-xx:maxpermsize=2096m- djava.util.logging.manager=org.apache.juli.classloaderlogmanager-djava.endorsed.dirs=/usr/local/tomcat7/ endorsed-dcatalina.base=/usr/local/tomcat7-dcatalina.home=/usr/local/tomcat7-djava.io.tmpdir=/usr/local/ Tomcat7/temp Org.apache.catalina.startup.Bootstrap
As above, we know that PID is 374, switch to log on using Tomcat user
VI jkiller.sh Enter the following script:
Copy Code code as follows:
#!/bin/sh
Export lang= "ZH_CN. UTF-8 ";
Export Lc_all= "ZH_CN. UTF-8 ";
Log_file= "/tmp/jkiller.log";
Jstack_file= "/tmp/jstack.log";
Pid= "$";
Shift
i=0;
J= "$";
If [-Z "${j}"]; Then
j=5;
Fi
Ps-mp ${pid}-o Thread,tid,time | Sort-rn > ${log_file};
Jstack ${pid} > ${jstack_file};
For line in ' Cat ${log_file}|gawk-f '-' {print $} ' |gawk-f ' ' {print '} '
Todo
i=$ (($i + 1));
if (($i > $j)); Then
Break
Fi
Xpid= ' printf '%x\n ' ${line} ';
Echo-ne "\033[32m";
Echo ${xpid};
Echo-e "\033[34m";
Grep-a "0x${xpid}" ${jstack_file};
Echo-e "\e[0m";
Done
Execute the script:
Copy Code code as follows:
The script will generate a/tmp/jkiller.log file, which contains the CPU usage of each thread, with TID in it, thread ID, which can be combined/tmp/jstack.log file to see the specific thread.