我怎么知道我是否在屏幕上?

在 Linux 中使用 screen 时,如何判断是否处于 screen 中? 我可以做 exit,我会退出一个屏幕,如果我在一个,但如果我不是,那么我将结束关闭我的终端。

在执行 screen -r时,我可以看到是否有其他屏幕连接,但是如何知道我当前的终端是否是那些连接的屏幕之一?

20553 次浏览

Check $STY. If it's null, you're on a "real" terminal. If it contains anything, it's the name of the screen you're in.

If you are not in screen:

eric@dev ~ $ echo $STY
eric@dev ~ $

If you are in screen:

eric@dev ~ $ echo $STY
2026.pts-0.ip-10-0-1-71

screen -ls can tell you.

Outside screen:

$ screen -ls
There are screens on:
16954.pts-1.auds916     (Detached)
242.pts-8.auds916       (Detached)
2 Sockets in /tmp/screens/S-glennj.

Inside a screen:

$ screen -ls
There are screens on:
16954.pts-1.auds916     (Attached)
242.pts-8.auds916       (Detached)
2 Sockets in /tmp/screens/S-glennj.

Another way I've done it is to echo $TERM.

 $ echo $TERM
screen

Since I end up doing this a lot, I added an alias into my .bashrc file:

alias trm='echo $TERM'

This way, whether in screen or not, if I just execute 'trm' it will show me whether I'm in SCREEN or elsewhere (usually XTERM).

Alternative approach to check if you are in screen.

type:

Ctrl-a ?

If you see the screen help you are in screen.

Otherwise you'll get a question mark '?' on the prompt.

Since all of the other methods here rely on environment variables (which can simply be overridden) or the command character for screen (which can also be overridden), the most foolproof way to check would be to list all the ancestors of the current process.

pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*/\1/' | grep screen | wc -l

If it prints 1, then the current process you're running has an ancestor with the word 'screen' in the executable's name, otherwise there wasn't.

A more facile visible inspection might be obtained from:

pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*/\1/' | less

While ssh'd into a remote (older) system I noticed that $TERM indicated I was using 'screen-256color', however there was no termcap/terminfo entry for that, so I was forced to resort to the following in .bashrc to prevent the terminal from producing occasional garbage:

case $TERM in
(screen-256color) export TERM='screen'
esac

to get it to use the plain entry instead.

TL;DR, $TERM will usually indicate if you are in a screen session when ssh'd remotely. You can use case $TERM in (screen*) echo "you are in a screen session"; esac if you just want a visual clue and don't need to do something specific

Add one or more of the followings into your .bashrc

  • alias mysession='echo ${STY}'
  • alias myterm='echo ${TERM}'
  • alias isscreen='if test -n "$STY"; then echo " screen session: ${STY}"; else echo " NOT a screen session"; fi'

Then you can know if you are inside a screen by typing simple commands.

The problem with most of the above answers is that we might be in a subshell of an attached screen session. Or we might be opening a shell to a remote host from within a screen session. In the former case, we can walk the process tree parentage and match for the screen program name. In the latter case, most of the time, we can check the TERM variable for something like screen*.

My answer os similar to /u/Parthian-Shot but not so dependent on the pstree utility; the options he use are not available to me. On the other hand, my implementation is still Linux-dependent: for non-Linux systems, one must tweak the ps command; for systems with older shells that don't support arrays, you'll have yet more work-arounds. But anyway:

ps_walk_parents() {
local tmp
local ppid=$PPID
while [[ $ppid != 1 ]]; do
tmp=($( ps -o ppid,comm -p $ppid ))
ppid=${tmp[0]}  # grab parent pid
echo ${tmp[1]}  # output corresponding command name
done
}
if [[ "$TERM" =~ screen* ]] || ps_walk_parents |grep -qxi screen ; then
# we are in a screen terminal
fi

We could optimize our function a bit to stop searching if/when a process parent matches the target command name ("screen"), but in general, the function will only hit 2 to 3 iterations. Presumably you want to put this code in some startup initialization such as .bashrc or .profile or something, so again, not worth optimizing.

My solution to the problem is a lot simpler: just hitting TAB makes the full terminal blink (a quick video inversion) if you are inside GNU Screen.

Tested working on most Linux (Ubuntu, Kali, Debian, RaspBerry... etc) and FreeBSD, GUI and any terminal, local or remote, including CtrlAltFn ones.

As an exception for this method, please, note this (rather complex, but possible) case scenario:

  • 1.- SSH into computer A (lets assume Linux).
  • 2.- Enter a new screen -S AScr from remote terminal on computer A.
  • 3.- SSH from GNU Screen AScr terminal into Computer B.
  • 4.- Enter a new screen -S BScr from remote terminal on computer B.

You are inside a Screen on cases 2 and 4, and outside a Screen on cases 1 and 3, but the terminal will blink on cases 2, 3 and 4.