zsh: better remote session detection with pstree

This commit is contained in:
Erin 2023-07-19 23:03:55 -04:00
parent aeac7bb99c
commit 166fc85d47

View file

@ -1,8 +1,10 @@
# Detect if the current session is running on a remote server
# Runs first because multiple other things rely on this
if [ -n "$IS_REMOTE_SESSION" ] || [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
is_remote=1
else
is_remote=
fi
export IS_REMOTE_SESSION=$is_remote
# Detect if the current session is running on a remote server by listing parent
# processes of this shell and checking for things like sshd/tailscaled. Runs
# first because multiple other things rely on this.
is_remote=""
proctree="$(pstree -s $$)"
echo "$proctree" | grep "sshd" && is_remote="$is_remote sshd"
echo "$proctree" | grep "tailscaled" && is_remote="$is_remote tailscaled"
[ -n "$is_remote" ] && export IS_REMOTE_SESSION="$(echo $is_remote | xargs)"