4

We used gnu screen a lot in the office and sometimes if I don't take note, I end up getting lost as to what screen session I already am in.

So my question is, how do you know what screen session you're in? Do you have a way to check?

CLARIFICATION: What I need to know is when I do a:

~$ screen -ls

and I see something like:

There is a screen on:
    4732.work_Sept42012     (Attached)
    3551.web01  (Attached)
    5521.mysql01 (Attached)
    1255.tomcat05 (Detached)
    3326.oracle15 (Attached)

 1 Socket in /tmp/uscreens/S-icasimpan.

How do I know if I am currently connected to any of the attached session?

Thanks in advance.

icasimpan
  • 1,253
  • 3
  • 16
  • 28
  • 1
    See [this answer](http://superuser.com/questions/212392/how-to-include-screens-session-name-in-hardstatus) from superuser.com – Burhan Khalid Sep 03 '12 at 02:26
  • @BurhanKhalid this is a good point but I wonder if there is screen generated info you can query. +1 for giving a helpful response :) – icasimpan Sep 04 '12 at 01:54
  • possible duplicate of [How can I tell whether I'm in a screen?](http://stackoverflow.com/questions/5392618/how-can-i-tell-whether-im-in-a-screen) – Maic López Sáenz Feb 14 '14 at 01:12

2 Answers2

7

In addition to the sessionname screen command, screen sets the STY environment variable for processes it starts. You can check the value of this variable from a shell:

$ echo $STY
5521.mysql01
$
bonsaiviking
  • 5,825
  • 1
  • 20
  • 35
4

Update:

ctrl+a, :sessionname can be used to display the name of the current screen session.

Previous reply (with missinterpreted question):

I struggled with this too until I came across the wonderful hardstatus feature. With these two lines in my ~/.screenrc I get the following at the bottom of my screen sessions.

hardstatus alwayslastline
hardstatus string '%{= kG}%-Lw%{= kW}%50> %n*%f %t%{= kG}%+Lw%< %{= kG}%-=%D %M %d | %C:%s %A %1`%{-}'

enter image description here

What I am looking at here is a screen session on my local computer with three windows (bash, mumble, kalkyl). In the active window called mumble I am connected to another computer over ssh on which I am running another screen session with three windows (top, auto, bash). I can see see all windows in both sessions and keep track of where I am.

Backlin
  • 14,612
  • 2
  • 49
  • 81
  • 1
    The purpose of having the current time in the status line is to see whether the connection is live, i.e. if I get disconnected the time freezes. – Backlin Sep 03 '12 at 09:20
  • your reply was helpful and I actually used it now in my .screenrc. I've added clarificatory statements to my question that you might have an answer too. +1 point for you too for being helpful :) – icasimpan Sep 04 '12 at 01:52
  • the ^A :sessionname method works when using sudo (or any other context that does not preserve the parent environment where $STY was set) – gorjusborg May 03 '16 at 15:59