@@ -19,12 +19,13 @@ Options:
1919 -p, --pid find out the highest cpu consumed threads from the specifed java process,
2020 default from all java process.
2121 -c, --count set the thread count to show, default is 5
22+ -u, --user set java process user
2223 -h, --help display this help and exit
2324EOF
2425 exit $1
2526}
2627
27- readonly ARGS=` getopt -n " $PROG " -a -o c:p:h -l count:,pid:,help -- " $@ " `
28+ readonly ARGS=` getopt -n " $PROG " -a -o c:p:u: h -l count:,pid:,help -- " $@ " `
2829[ $? -ne 0 ] && usage 1
2930eval set -- " ${ARGS} "
3031
@@ -38,6 +39,10 @@ while true; do
3839 pid=" $2 "
3940 shift 2
4041 ;;
42+ -u|--user)
43+ jvmuser=" $2 "
44+ shift 2
45+ ;;
4146 -h|--help)
4247 usage
4348 ;;
@@ -93,14 +98,25 @@ printStackOfThread() {
9398 local pcpu=` echo ${threadLine} | awk ' {print $5}' `
9499
95100 local jstackFile=/tmp/${uuid} _${pid}
96-
101+
102+
97103 [ ! -f " ${jstackFile} " ] && {
98- jstack ${pid} > ${jstackFile} || {
99- redEcho " Fail to jstack java process ${pid} !"
100- rm ${jstackFile}
101- continue
102- }
103- }
104+ if [ ` uname -m` == " x86_64" ]; then
105+ flag=" -J-d64" # x86-64 specify jstack option "-J-d64"
106+ fi
107+ if [ -z ${jvmuser+x} ]; then
108+ # root user
109+ jstackCmd=" $JAVA_HOME /bin/jstack $flag "
110+ else
111+ # non-root user use sudo -u user
112+ jstackCmd=" sudo -u $jvmuser $JAVA_HOME /bin/jstack $flag "
113+ fi
114+ $jstackCmd ${pid} > ${jstackFile} || {
115+ redEcho " Fail to jstack java process ${pid} !"
116+ rm ${jstackFile}
117+ continue
118+ }
119+ }
104120
105121 redEcho " [$(( count++ )) ] Busy(${pcpu} %) thread(${threadId} /0x${threadId0x} ) stack of java process(${pid} ) under user(${user} ):"
106122 sed " /nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}
0 commit comments