305

I have a bunch of servers, on which I run experiments using screen. The procedure is the following :

  1. ssh to server XXX
  2. launch screen
  3. start experiments in a few tabs
  4. detach screen
  5. disconnect from the server

While the experiments are running, I can easily find on which servers they are by sshing to all servers and listing my running processes (using top or ps).

However, once the experiments are finished, how could I find on which servers I have a screen session opened (so that I can have a look at the output, relaunch them, etc.) ?

PS: my experiments do print their output to files, too... but this is not the point of my question.

Drew Stephens
  • 17,207
  • 15
  • 66
  • 82
Wookai
  • 20,883
  • 16
  • 73
  • 86

11 Answers11

525

To list all of the screen sessions for a user, run the following command as that user:

screen -ls

To see all screen sessions on a specific machine you can do:

ls -laR /var/run/screen/

I get this on my machine:

gentle ~ # ls -laR /var/run/screen/

/var/run/screen/:
total 1
drwxrwxr-x  4 root utmp   96 Mar  1  2005 .
drwxr-xr-x 10 root root  840 Feb  1 03:10 ..
drwx------  2 josh users  88 Jan 13 11:33 S-josh
drwx------  2 root root   48 Feb 11 10:50 S-root

/var/run/screen/S-josh:
total 0
drwx------ 2 josh users 88 Jan 13 11:33 .
drwxrwxr-x 4 root utmp  96 Mar  1  2005 ..
prwx------ 1 josh users  0 Feb 11 10:41 12931.pts-0.gentle

/var/run/screen/S-root:
total 0
drwx------ 2 root root 48 Feb 11 10:50 .
drwxrwxr-x 4 root utmp 96 Mar  1  2005 ..

This is a rather brilliantly Unixy use of Unix Sockets wrapped in filesystem permissions to handle security, state, and streams.

joshperry
  • 41,167
  • 16
  • 88
  • 103
  • 2
    I wonder why this answer, one that answered the question perfectly, got a downvote... – Blaszard Oct 22 '16 at 18:24
  • 2
    Alternatively if you're wanting to get a specific screen based on a naming convention you can grep the result. `ls -R /var/run/screen/S-root/ | grep "NamingConvention"`. Screens save in the `.` format. This is useful for bash scripts. So if you have a screen with name `0000.NamingConvention` it'll literally just return the name of the screens you're looking for in an easy to parse format. – Elias Ranz Aug 16 '18 at 14:32
28

Simple guide to remember...

screen - To create a screen

screen -list - List all the detached (running) screens with their screen IDs.

enter image description here in this picture -

6764.pts-1.v1091330

is a screen ID.

screen -x [screen id] - Connect / Attach to a specific running screen.

Ctrl + D - while in a screen to Terminate / Stop a screen from running.

Ctrl + A, Then press D - while in a screen to detach from screen without disturbing it.

killall screen - Detach or terminate all screens.

MBK
  • 2,589
  • 21
  • 25
24

The command screen -list may be what you want.

See the man

Zoredache
  • 37,543
  • 7
  • 45
  • 61
14

While joshperry's answer is correct, I find very annoying that it does not tell you the screen name (the one you set with -t option), that is actually what you use to identify a session. (not his fault, of course, that's a screen's flaw)

That's why I instead use a script such as this: ps auxw|grep -i screen|grep -v grep

o0'.
  • 11,739
  • 19
  • 60
  • 87
  • 2
    Take a look at the -S option. The value provided is stored in the unix socket filename and is visible in `ls` output. If you need, you can keep the -t option as well. – joshperry Apr 23 '17 at 18:27
  • The -S option is great, as it also lets you resume by that session name: `screen -rd ` – jorisw May 16 '20 at 09:15
  • 1
    grep knows regular expressions: ps auxw|grep -i [s]creen – Jared Still Jun 10 '22 at 14:48
13

I'm not really sure of your question, but if all you really want is list currently opened screen session, try:

screen -ls
skinp
  • 4,157
  • 4
  • 27
  • 20
8
 For windows system

 Open putty 
 then login in server

If you want to see screen in Console then you have to write command

 Screen -ls

if you have to access the screen then you have to use below command

 screen -x screen id

Write PWD in command line to check at which folder you are currently

Ankit jain
  • 4,198
  • 4
  • 19
  • 24
5

Multiple folks have already pointed that

$ screen -ls

would list the screen sessions.

Here is another trick that may be useful to you.

If you add the following command as a last line in your .bashrc file on server xxx, then it will automatically reconnect to your screen session on login.

screen -d -r

Hope you find it useful.

PHP Bugs
  • 1,133
  • 12
  • 23
samurai
  • 51
  • 1
  • 2
    I want to note that you'll have to be logged in as the same user that started the screen, I was worried my screen session was gone, but my file copy was continuing under the user "media". It may sound logical, but it could save some panic if you oversee it. – Paul Mar 09 '18 at 18:51
5

In most cases a screen -RRx $username/ will suffice :)

If you still want to list all screens then put the following script in your path and call it screen or whatever you like:

#!/bin/bash
if [[ "$1" != "-ls-all" ]]; then
    exec /usr/bin/screen "$@"
else
    shopt -s nullglob
    screens=(/var/run/screen/S-*/*)
    if (( ${#screens[@]} == 0 )); then
        echo "no screen session found in /var/run/screen"
    else
        echo "${screens[@]#*S-}"
    fi
fi

It will behave exactly like screen except for showing all screen sessions, when giving the option -ls-all as first parameter.

ObiWahn
  • 111
  • 1
  • 3
1

You could use the below commands.

screen -list

(or)

screen -R
SuperNova
  • 25,512
  • 7
  • 93
  • 64
0

ps x | grep SCREEN

to see what is that screen running in case you used the command

screen -A -m -d php make_something.php

-6

So you're using screen to keep the experiments running in the background, or what? If so, why not just start it in the background?

./experiment &

And if you're asking how to get notification the job i done, how about stringing the experiment together with a mail command?

./experiment && echo "the deed is done" | mail youruser@yourlocalworkstation -s "job on server $HOSTNAME is done"
JosefAssad
  • 4,018
  • 28
  • 37
  • I'm using screen so that I can disconnect from the servers without terminating my experiments. The question was not how to get notifications (though the mail could be a nice way of doing it), but how to find if a given machine had an opened screen session. Thanks anyway for your answer ! – Wookai Feb 11 '09 at 19:18
  • I'm not an experienced sysadmin, but I find using screen to run server processes works well: if something goes wrong, you can just flick to that window, and see all the output that led to the problem - you don't need to start opening up log files. It also means you can easily kill a process with ctrl+c. This is all handy if you're frequently starting and shutting down the same service. – Steve Bennett Apr 05 '11 at 08:37