@@ -18,14 +18,14 @@ readonly PROG_VERSION='2.x-dev'
18
18
# Will $0 always include the path to the script?
19
19
# https://unix.stackexchange.com/questions/119929
20
20
readonly -a COMMAND_LINE=(" ${BASH_SOURCE[0]} " " $@ " )
21
- # Get current user name via whoami command
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 rewritten/correct, just inherited from outside!
21
+ # CAUTION: env var $USER is not reliable!
22
+ # $USER may be overwritten; if run command by `sudo -u`, may is not `root`.
23
+ # more info see https://www.baeldung.com/linux/get-current-user
24
24
#
25
- # DO NOT declare and assign var USER (as readonly) in ONE line!
25
+ # DO NOT declare and assign var WHOAMI (as readonly) in ONE line!
26
26
# more info see https://github.com/koalaman/shellcheck/wiki/SC2155
27
- USER =$( whoami)
28
- readonly USER
27
+ WHOAMI =$( whoami)
28
+ readonly WHOAMI
29
29
30
30
# ###############################################################################
31
31
# util functions
@@ -525,7 +525,7 @@ printStackOfThreads() {
525
525
[ -f " $jstackFile " ] || {
526
526
# shellcheck disable=SC2206
527
527
local -a jstack_cmd_line=(" $jstack_path " $force $mix_native_frames $lock_info $pid )
528
- if [ " $user " = " $USER " ]; then
528
+ if [ " $user " = " $WHOAMI " ]; then
529
529
# run without sudo, when java process user is current user
530
530
logAndRun " ${jstack_cmd_line[@]} " > " $jstackFile "
531
531
elif (( UID == 0 )) ; then
@@ -534,7 +534,7 @@ printStackOfThreads() {
534
534
else
535
535
# current user is not root user, so can not run with sudo; print error message and rerun suggestion
536
536
redOutput " [$idx ] Fail to jstack busy($pcpu %) thread($threadId /$threadId0x ) stack of java process($pid ) under user($user )."
537
- redOutput " User of java process($user ) is not current user($USER ), need sudo to rerun:"
537
+ redOutput " User of java process($user ) is not current user($WHOAMI ), need sudo to rerun:"
538
538
yellowOutput " sudo $( printCallingCommandLine) "
539
539
continue
540
540
fi || {
0 commit comments