How to Detect and Display Screen/Tmux Session Status in Linux Shell Prompt


2 views

As Linux system administrators and developers, we often need to know whether our current shell session is running inside a terminal multiplexer (like GNU Screen or tmux). This is crucial because:

  • Multiplexed sessions persist after terminal disconnection
  • We can safely close terminal windows without losing work
  • Session recovery is possible after network interruptions

The most reliable way to check for GNU Screen is to examine the STY environment variable or look for screen-specific process information:

# Method 1: Check STY environment variable
if [ -n "$STY" ]; then
    echo "Running inside GNU Screen session: $STY"
fi

# Method 2: Check process tree
if ps -p $PPID | grep -q screen; then
    echo "Parent process is screen"
fi

For tmux users, we check the TMUX environment variable:

if [ -n "$TMUX" ]; then
    echo "Running inside tmux session"
fi

Here's a combined function that works for both screen and tmux:

in_terminal_multiplexer() {
    [ -n "$STY" ] || [ -n "$TMUX" ] || pstree -s $$ | grep -wq 'screen\\|tmux'
}

To make this information visible in your PS1 prompt, add this to your .bashrc:

set_multiplexer_indicator() {
    if [ -n "$STY" ]; then
        printf "[screen:%s]" "${STY#*.}"
    elif [ -n "$TMUX" ]; then
        printf "[tmux]"
    fi
}

export PS1='$$\e[32m$$\u@\h$$\e[0m$$:$$\e[34m$$\w$$\e[0m$$$(set_multiplexer_indicator)\$ '

For a more sophisticated solution with color coding:

set_prompt_multiplexer() {
    local multiplexer=""
    if [ -n "$STY" ]; then
        multiplexer="$$\033[1;33m$$[screen:${STY#*.}]$$\033[0m$$"
    elif [ -n "$TMUX" ]; then
        multiplexer="$$\033[1;36m$$[tmux]$$\033[0m$$"
    fi
    echo "$multiplexer"
}

export PS1='\[\e[32m\]\u@\h$$\e[0m$$:$$\e[34m$$\w$$\e[0m$$$(set_prompt_multiplexer)\$ '

Some other approaches worth considering:

# Using pstree (works without environment variables)
pstree -s $$ | grep -q 'screen\\|tmux' && echo "In multiplexer"

# Checking terminal type
case "$TERM" in
    screen*) echo "Screen session detected" ;;
    tmux*) echo "Tmux session detected" ;;
esac
  • Environment variables might not propagate through sudo - use sudo -E
  • Some systems might alias ps differently - use full path /bin/ps
  • In minimal environments, pstree might not be available

As a developer working with remote servers, you've probably encountered situations where an unexpected network disconnect or terminal closure abruptly terminates your SSH session. GNU Screen provides session persistence, but how can you programmatically determine if you're operating within a Screen session?

The most reliable method checks for Screen's environment variables:

if [ -n "$STY" ]; then
    echo "Running inside Screen (session: $STY)"
else
    echo "Not running inside Screen"
fi

The $STY variable contains the session information when running under Screen. This method works across most Linux/Unix systems.

For systems where environment variables might not be available, examine the process tree:

if pstree -s $$ | grep -q screen; then
    echo "Screen session detected"
fi

To make this status always visible, add this to your .bashrc:

# Add Screen status to PS1
screen_prompt() {
    [ -n "$STY" ] && echo "[Screen:${STY#*.}]" || echo ""
}
export PS1='\u@\h:\w$(screen_prompt)\$ '

This will display your Screen session name in brackets when applicable.

Some systems support these additional checks:

# Check terminal type
if [ "$TERM" = "screen" ]; then
    echo "Likely in Screen session"
fi

# Check for parent process
if [ "$(ps -o comm= -p $PPID)" = "screen" ]; then
    echo "Parent process is Screen"
fi

Knowing your Screen status enables smart scripting:

# Only attempt to reattach if in Screen
if [ -n "$STY" ]; then
    screen -d -r
else
    echo "No Screen session to reattach to"
fi

Be aware that:

  • Nested Screen sessions will only show the outermost session
  • Some systems may require screen in PATH for process checks
  • Custom compiled Screen versions might use different environment variables