@@ -19,8 +19,8 @@ uname | grep '^Linux' -q || {
1919}
2020
2121# Get corrent current user name via whoami command
22- # See get https://www.lifewire.com/current-linux-user-whoami-command-3867579
23- # Because if use `sudo -u` to run command , env var $USER is not rewrited/correct, just inherited from outside!
22+ # See https://www.lifewire.com/current-linux-user-whoami-command-3867579
23+ # Because if run command by `sudo -u`, env var $USER is not rewrited/correct, just inherited from outside!
2424readonly USER=" ` whoami` "
2525
2626usage () {
5454 exit $1
5555}
5656
57+ # ###############################################################################
58+ # parse options
59+ # ###############################################################################
60+
5761readonly ARGS=` getopt -n " $PROG " -a -o p:c:a:s:Fmlh -l count:,pid:,append-file:,jstack-path:,force,mix-native-frames,lock-info,help -- " $@ " `
5862[ $? -ne 0 ] && usage 1
5963eval set -- " ${ARGS} "
@@ -103,19 +107,20 @@ update_delay=${1:-0}
103107[ -z " $1 " ] && update_count=1 || update_count=${2:- 0}
104108[ $update_count -lt 0 ] && update_count=0
105109
110+ # ###############################################################################
111+ # utils funtions
112+ # ###############################################################################
113+
106114# NOTE: $'foo' is the escape sequence syntax of bash
107115readonly ec=$' \033 ' # escape char
108116readonly eend=$' \033 [0m' # escape end
109117
110118colorPrint () {
111119 local color=$1
112120 shift
113- if [ -t 1 ] ; then
114- # if stdout is console, turn on color output.
115- echo " $ec [1;${color} m$@ $eend "
116- else
117- echo " $@ "
118- fi
121+
122+ # if stdout is console, turn on color output.
123+ [ -t 1 ] && echo " $ec [1;${color} m$@ $eend " || echo " $@ "
119124
120125 [ -n " $append_file " ] && echo " $@ " >> " $append_file "
121126}
@@ -142,13 +147,16 @@ normalPrint() {
142147 [ -n " $append_file " ] && echo " $@ " >> " $append_file "
143148}
144149
150+ # ###############################################################################
151+ # check the existence of jstack command!
152+ # ###############################################################################
153+
145154if [ -n " $jstack_path " ]; then
146155 [ ! -x " $jstack_path " ] && {
147156 redPrint " Error: $jstack_path is NOT found/executalbe!" 1>&2
148157 exit 1
149158 }
150159elif which jstack & > /dev/null; then
151- # Check the existence of jstack command!
152160 jstack_path=" ` which jstack` "
153161else
154162 [ -z " $JAVA_HOME " ] && {
@@ -166,6 +174,10 @@ else
166174 jstack_path=" $JAVA_HOME /bin/jstack"
167175fi
168176
177+ # ###############################################################################
178+ # biz logic
179+ # ###############################################################################
180+
169181readonly uuid=` date +%s` _${RANDOM} _$$
170182
171183cleanupWhenExit () {
@@ -192,14 +204,14 @@ printStackOfThreads() {
192204 elif [ $UID == 0 ]; then
193205 sudo -u " ${user} " " $jstack_path " ${force} $mix_native_frames $more_lock_info ${pid} > ${jstackFile}
194206 else
195- redPrint " [$counter ] Fail to jstack Busy (${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} )."
207+ redPrint " [$counter ] Fail to jstack busy (${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} )."
196208 redPrint " User of java process($user ) is not current user($USER ), need sudo to run again:"
197209 yellowPrint " sudo ${COMMAND_LINE[@]} "
198210 normalPrint
199211 continue
200212 fi
201213 } || {
202- redPrint " [$counter ] Fail to jstack Busy (${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} )."
214+ redPrint " [$counter ] Fail to jstack busy (${pcpu} %) thread(${threadId} /${threadId0x} ) stack of java process(${pid} ) under user(${user} )."
203215 normalPrint
204216 rm ${jstackFile}
205217 continue
0 commit comments