Skip to content

Commit 8f2487e

Browse files
committed
refactor(show-busy-java-threads): rename global var USER -> WHOAMI 👤
1 parent 906ec30 commit 8f2487e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

bin/show-busy-java-threads

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ readonly PROG_VERSION='2.x-dev'
1818
# Will $0 always include the path to the script?
1919
# https://unix.stackexchange.com/questions/119929
2020
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
2424
#
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!
2626
# more info see https://github.com/koalaman/shellcheck/wiki/SC2155
27-
USER=$(whoami)
28-
readonly USER
27+
WHOAMI=$(whoami)
28+
readonly WHOAMI
2929

3030
################################################################################
3131
# util functions
@@ -525,7 +525,7 @@ printStackOfThreads() {
525525
[ -f "$jstackFile" ] || {
526526
# shellcheck disable=SC2206
527527
local -a jstack_cmd_line=("$jstack_path" $force $mix_native_frames $lock_info $pid)
528-
if [ "$user" = "$USER" ]; then
528+
if [ "$user" = "$WHOAMI" ]; then
529529
# run without sudo, when java process user is current user
530530
logAndRun "${jstack_cmd_line[@]}" >"$jstackFile"
531531
elif ((UID == 0)); then
@@ -534,7 +534,7 @@ printStackOfThreads() {
534534
else
535535
# current user is not root user, so can not run with sudo; print error message and rerun suggestion
536536
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:"
538538
yellowOutput " sudo $(printCallingCommandLine)"
539539
continue
540540
fi || {

0 commit comments

Comments
 (0)