How to List Running Screen Sessions

How to list running screen sessions?

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.

Checking any screen session active on linux

Just grep for the proper string:

screen -list | grep "No active:" && echo "No active Program" || echo "There is an active Program"

How to monitor running screen session and start a new session once last one ended?

#!/bin/bash

doit() {
if [ -f "/opt/anaconda/etc/profile.d/conda.sh" ]; then
. "/opt/anaconda/etc/profile.d/conda.sh"
CONDA_CHANGEPS1=false conda activate PyTorchNN
echo "Activated conda env"
fi
echo $1
python main_broad_FEA.py --hyperparam-json $2 --GPU $3 --varstop $4
}
export -f doit

parallel -j 3 {1} {2} {%} {3} ::: a r g 1 ::: a r g 2 ::: a r g 4

Explanation:

  • "-j 3" defines the number of job slots
  • "{1}" is replaced by the respective element from "arg1" - likewise for {2} and {3}
  • {%} is the job slot number. We use this to determine which GPU to run on. (See page 30 of https://doi.org/10.5281/zenodo.1146014)
  • ":::" is followed by a list of arguments (one per job)

If you want to monitor the running jobs live, you can do that using tmux:

parallel --tmux -j 3 {1} {2} {%} {3} ::: a r g 1 ::: a r g 2 ::: a r g 4

How to get into screen session?

Reattach to the session:

screen -r <session_id>

Example:

  1. Create a session, give it a nice name:

    user:~$ screen -S nananananana_batman
  2. Close the terminal (detach).

  3. List the active sessions:

    user:~$ screen -list

    There is a screen on:
    3151.nananananana_batman (10/23/2013 05:34:02 PM) (Detached)
    1 Socket in /var/run/screen/S-user.
  4. Reattach back to our session:

    user:~$ screen -S nananananana_batman

opening all detached screen sessions in one command on linux

It would be easier to have only one screen session with many terminals within. You create a new terminal with Ctrl+a+c, then you switch through them with Ctrl+a+<space> or <backspace> or a number.
I don't think there is an easier way if you do that manually. If you spawn sessions with a script, you may try using -X to make screen open a new window in an existing session and execute commands in there. Perhaps this answer is close to what you would need. You will need to make sure you name your screen session in a way, you can reference it later unambiguously.



Related Topics



Leave a reply



Submit