How can I find out if I am on the screen?

When using the screen in Linux, how can I find out if I am on the screen or not? I could exit , and I will exit screen if I was in one, but if I were not, then I will finish closing the terminal.

When running screen -r I could see if I have other screens, but how do I know if my current terminal is one of these connected screens?

+70
linux shell gnu-screen
Mar 22 2018-11-22T00:
source share
10 answers

Check out $STY . If it is zero, you are on the "real" terminal. If it contains anything, this is the name of the screen you are in.

If you are not on the screen:

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

If you are on the screen:

 eric@dev ~ $ echo $STY 2026.pts-0.ip-10-0-1-71 
+115
Mar 22 '11 at 14:17
source share

Another way I did this is to repeat $ TERM.

  $ echo $TERM screen 

Since I do this very often, I added an alias to my .bashrc file:

 alias trm='echo $TERM' 

That way, on the screen or not, if I just execute "trm", it will show me whether I am on the screen or in another place (usually XTERM).

+15
Aug 09 2018-12-12T00:
source share

An alternative approach to check if you are on the screen.

Type of:

 Ctrl-a ? 

If you see the on-screen help on the screen.

Otherwise, you will receive a question mark '?' on the command line.

+11
Jan 27 '14 at 14:23
source share

Just enter echo $STY ; this will return an attached screen with a process id for example

 $ echo $STY 34046.myScreen 
+7
Dec 06
source share

Since all other methods here are based on environment variables (which you can simply override) or a command character for the screen (which can also be overridden), the most reliable 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 that you are using has an ancestor with the word “screen” in the executable name, otherwise it didn’t.

Easier visible inspection can be obtained from:

 pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*/\1/' | less 
+2
Jul 08 '15 at 18:26
source share

While ssh'd to a remote (older) system, I noticed that $ TERM indicated that I used 'screen-256color', however there was no termcap / terminfo entry for this, so I had to resort to the following :. bashrc to prevent the terminal from accidentally causing garbage:

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

to use regular recording instead.

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

+1
Sep 09 '15 at 15:03
source share

Add one or more of the following to .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 find out if you are inside the screen by typing simple commands.

+1
Aug 29 '16 at 17:06
source share

The problem with most of the above answers is that we may be in a subshell of the connected screen session. Or we can open a shell on a remote host from a screen session. In the first case, we can track the origin of the process tree and match the name of the screen program. In the latter case, most of the time we can check the TERM variable for something like screen* .

My os answer is similar to / u / Parthian -Shot, but not so much dependent on the pstree utility; the options that he uses are not available to me. On the other hand, my implementation is still Linux-dependent: for non-Linux systems, you need to configure the ps command; for systems with old shells that do not support arrays, you will have even more options for work. 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 the search if / when the parent of the process matches the name of the target command ("screen"), but in general the function will perform only 2 to 3 iterations. Presumably you want to put this code in some startup initialization, such as .bashrc or .profile or something else, so you should not optimize.

0
Sep 22 '16 at 16:47
source share

My solution to the problem is much simpler: just pressing TAB makes the full terminal blink (fast video inversion) if you are inside the GNU screen.

Tested while working on most Linux (Ubuntu, Kali, Debian, RaspBerry ... etc.) and FreeBSD , a graphical interface and any terminal, local or remote, including Ctrl Alt Fn .

As an exception to this method, please pay attention to this (rather complicated, but possible) scenario:

  • 1.- SSH to computer A (Linux suggests).
  • 2.- Enter the new screen -S AScr from the remote terminal on computer A.
  • 3.- SSH from the GNU Screen AScr to computer B.
  • 4.- Enter the new screen -S BScr from the remote terminal on computer B.

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

0
Oct. 20 '17 at 23:02 on
source share

screen -ls can tell you.

External 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 the screen:

 $ screen -ls There are screens on: 16954.pts-1.auds916 (Attached) 242.pts-8.auds916 (Detached) 2 Sockets in /tmp/screens/S-glennj. 
-3
Mar 22 2018-11-22T00:
source share



All Articles