Using shell scripts in Linux to view CPU usage of Java Threads _linux Shell

Source: Internet
Author: User
Tags thread class cpu usage

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:
./jkiller.sh 374

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.