How to Detect If You’re Inside a GNU Screen Session to Avoid Accidental Disconnection


2 views

When working on remote servers through SSH, many developers use GNU Screen or tmux for session persistence. A critical concern arises: if you accidentally press Ctrl+D, will it terminate your SSH connection or just exit the current screen window? Knowing your environment is crucial for workflow safety.

Here are several reliable ways to check if you're inside a screen session:

# Method 1: Check the $TERM variable
echo $TERM
# Output for screen sessions will typically be 'screen' or 'screen-256color'

# Method 2: Check for STY environment variable
echo $STY
# Returns session PID if in screen, empty otherwise

# Method 3: Look for screen-specific process
ps -p $PPID -o comm=
# Returns 'screen' if parent process is screen

For scripting purposes, you might want a bash function to test this:

function in_screen() {
    [ -n "$STY" ] || [ "$TERM" = "screen" ] || [ "$TERM" = "screen-256color" ]
}

if in_screen; then
    echo "You're in a screen session - Ctrl+D will just close this window"
else
    echo "You're NOT in screen - Ctrl+D will disconnect!"
fi

For more complex environments or containerized workflows:

# Method 4: Check parent process tree
pstree -s $$ | grep -q screen && echo "In screen" || echo "Not in screen"

# Method 5: Alternative environment variable check
[ -n "$WINDOW" ] && echo "Screen detected" || echo "No screen"

Note that these methods work for GNU Screen specifically. If you're using tmux instead, different environment variables apply (TMUX instead of STY). For a multiplexer-agnostic check:

function in_multiplexer() {
    [ -n "$STY" ] || [ -n "$TMUX" ] || \
    [[ "$TERM" =~ ^(screen|tmux) ]] || \
    (ps -o comm= -p $PPID | grep -qE '^(screen|tmux)')
}

Many developers customize their shell prompts to show multiplexer status. For bash/zsh:

export PS1='${debian_chroot:+($debian_chroot)}$$\033[01;32m$$\u@\h$$\033[00m$$:$$\033[01;34m$$\w$$\033[00m$$$(if [ -n "$STY" ]; then echo " [screen:$STY]"; fi)\$ '

This will display your current screen session ID in the prompt when inside screen.

Beyond detection, consider these protective measures:

# Make Ctrl+D require two consecutive presses
IGNOREEOF=10  # Requires 10 Ctrl+D presses before logout
# Add to ~/.bashrc or equivalent

Every Linux sysadmin has faced this moment: your finger hovers over Ctrl+D, unsure whether it will simply detach your current screen session or completely disconnect you from the remote server. This uncertainty can be dangerous when working on production systems.

Here are three reliable ways to check for an active screen session:

# Method 1: Check the $TERM environment variable
echo $TERM
# If inside screen, output will be 'screen' or 'screen.xterm-256color'

# Method 2: Look for screen-specific environment variables
env | grep -i screen
# Typical output when inside screen:
# STY=1234.pts-0.hostname
# WINDOW=0

# Method 3: The most definitive check
if [ -n "$STY" ]; then
    echo "You're in screen session $STY"
else
    echo "Not in screen"
fi

For daily use, add this to your ~/.bashrc:

screen_prompt() {
    if [ -n "$STY" ]; then
        echo -n "[screen:$STY]"
    fi
}
export PS1="\$(screen_prompt)$PS1"

This modification will display your screen session ID in the prompt when inside screen, providing constant visual confirmation.

For even better visibility, you can modify the terminal window title:

case "$TERM" in
    screen*)
        PROMPT_COMMAND='echo -ne "\033k${HOSTNAME%%.*}\033\\"'
        ;;
    *)
        PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}\007"'
        ;;
esac

Accidental disconnections can:

  • Terminate long-running processes
  • Lose unsaved work in text editors
  • Require re-authentication to servers

By implementing these checks, you'll always know exactly what Ctrl+D will do before you press it.