Skip to content

Commit d0867e4

Browse files
committed
add busy-java-threads.sh and find-in-jars.sh
0 parents  commit d0867e4

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
useful-shells
2+
==================
3+
4+
把平时有用的手动操作做成脚本,这样可以便捷的使用。
5+
6+
busy-java-threads.sh
7+
=======================
8+
9+
在排查`Java``CPU`性能问题时,需要找到消耗`CPU`的线程,查看它的线程栈。
10+
11+
这个脚本的功能是,打印出在运行的`Java`进程中,消耗`CPU`最多的那5个线程的线程栈。
12+
13+
示例:
14+
15+
```bash
16+
$ ./busy-java-threads.sh
17+
The stack of busy(0.0%) thread(30509/0x772d) of java process(29213) of user(foo):
18+
"Attach Listener" daemon prio=10 tid=0x0000000042171800 nid=0x772d waiting on condition [0x0000000000000000]
19+
java.lang.Thread.State: RUNNABLE
20+
21+
The stack of busy(0.0%) thread(29230/0x722e) of java process(29213) of user(foo):
22+
"GC Daemon" daemon prio=10 tid=0x00007f21340ec800 nid=0x722e in Object.wait() [0x00007f2133ae3000]
23+
java.lang.Thread.State: TIMED_WAITING (on object monitor)
24+
at java.lang.Object.wait(Native Method)
25+
- waiting on <0x00000000a0000120> (a sun.misc.GC$LatencyLock)
26+
at sun.misc.GC$Daemon.run(GC.java:100)
27+
- locked <0x00000000a0000120> (a sun.misc.GC$LatencyLock)
28+
29+
...
30+
```
31+
32+
33+
find-in-jars.sh
34+
===================
35+
36+
在当前目录下所有`Jar`文件里,查找文件名。
37+
38+
用法:
39+
40+
```bash
41+
find-in-jars.sh 'sofa\.properties'
42+
find-in-jars.sh log4j\\.xml
43+
find-in-jars.sh 'log4j\.properties|log4j\.xml'
44+
```
45+
46+
注意,后面Pattern是`grep`的扩展正则表达式。
47+
48+
示例:
49+
50+
```bash
51+
$ ./find-in-jars '*Service.class'
52+
./spring-2.5.6.SEC03.jar!org/springframework/stereotype/Service.class
53+
./rpc-benchmark-0.0.1-SNAPSHOT.jar!com/taobao/rpc/benchmark/service/HelloService.class
54+
```
55+
56+
说明详见:[在多个Jar(Zip)文件查找Log4J配置文件的Shell命令行](http://oldratlee.com/458/tech/shell/find-file-in-jar-zip-files.html)

busy-java-threads.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
# @Function
3+
# Find the High cpu consume thread of java, and print the stack of these threads.
4+
#
5+
# @Usage
6+
# $ ./busy-java-threads.sh
7+
#
8+
# @author Jerry Lee
9+
10+
redEcho()
11+
{
12+
echo -e "\033[1;31m$@\033[0m"
13+
}
14+
15+
uuid=`date +%s`_${RANDOM}_$$
16+
17+
ps -Leo pid,lwp,user,comm,pcpu --no-headers | awk '$4=="java"{print $0}' |
18+
sort -k5 -r -n | head -5 | while read threadLine ; do
19+
pid=`echo ${threadLine} | awk '{print $1}'`
20+
threadId=`echo ${threadLine} | awk '{print $2}'`
21+
threadId0x=`printf %x ${threadId}`
22+
user=`echo ${threadLine} | awk '{print $3}'`
23+
pcpu=`echo ${threadLine} | awk '{print $5}'`
24+
25+
jstackFile=/tmp/${uuid}_${pid}
26+
27+
[ ! -f "${jstackFile}" ] &&
28+
{
29+
jstack ${pid} > ${jstackFile} ||
30+
{ redEcho "Fail to jstack java process ${pid}"; rm ${jstackFile} ; continue; }
31+
}
32+
33+
redEcho "The stack of busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) of java process(${pid}) of user(${user}):"
34+
sed "/nid=0x${threadId0x}/,/^$/p" -n ${jstackFile}
35+
done
36+
37+
rm /tmp/${uuid}_*

find-in-jars.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
# @Function
3+
# Find file in the jar files under current directory
4+
#
5+
# @Usage
6+
# $ find-in-jars.sh log4j\\.xml
7+
# $ find-in-jars.sh 'log4j\.properties'
8+
# $ find-in-jars.sh 'log4j\.properties|log4j\.xml'
9+
#
10+
# @author Jerry Lee
11+
12+
[ -z "$1" ] && { echo No find file pattern! ; exit 1; }
13+
14+
find -iname '*.jar' | while read jarFile
15+
do
16+
jar tf ${jarFile} | egrep $1 | while read file
17+
do
18+
echo "${jarFile}"\!"${file}"
19+
done
20+
done

0 commit comments

Comments
 (0)