General Information
Screen is a “full-screen window manager that multiplexes a physical terminal between several processes”. (screen man page)
This allows you to have multiple virtual terminal “workspaces” within 1 actual terminal session.
Checklist
yum install screen or apt-get install screen
2) Start a screen session
screen -S MySession
Screen may or may not show some startup dialog, but once you are past that, it will look just like a normal terminal prompt.
3) To view a list of all key bindings:
Ctrl+a, then ?
The listed commands have multiple ways of being executed. You only have to press “Ctrl+a” and then the key letter for the desired action. Press Space ore Enter to leave the help screen.
Start top in one screen, create a new screen, and cycle between the two.
Start top
top
Create a new screen
Ctrl+a, then c
You will be on the new screen session. Do something else to see the difference:
free -m
Now, you can cycle between the two screen sessions by doing this:
Ctrl+a, then n
You will notice that top was never interrupted, it was running the entire time on its screen session.
The real power of screen is detaching a session and connecting back to it later.
Detach scenarios:
In either scenario, you can reconnect to your still running screen session without loosing any work. Perfect for long running processes or scripts.
This can be demonstrated by executing a for loop that echos a number, sleeps for 1 second, and then keeps going.
1) If it is not open still, start screen
screen -s MySession
2) Start a long for loop…this should take about 5 minutes to complete.
for NUM in {1..300} do echo -e "Number is: ${NUM}" echo "Sleeping for 1 second..." sleep 1 done
3) Detach from screen after it has started.
Ctrl+a, then d
4) List screen processes for your user.
screen -ls
This will give you output similar to the following:
bill@dt-bill ~ $ screen -ls There is a screen on: 19734.MySession (03/09/2015 11:13:38 PM) (Detached) 1 Socket in /var/run/screen/S-bill. ps -ef | grep 19734 bill 19734 1 0 23:13 ? 00:00:00 SCREEN -S MySession
You can see that the number is indeed a process ID.
5) Reattach to the screen using the session name or process id:
screen -r MySession or screen -r 19734
Note: You only have to specify the screen PID if you have multiple screen sessions detached and you are connecting to a specific one. (If only 1 screen session, you can just type “screen -r”)
You should find your still running for loop, chugging away.
If you started a new screen session without specifying a name or want to rename a session:
1) Attach to your session
screen -r MySession
2) Command Key, then colon
Ctrl+A :
3) Type sessionname MyNewName, then enter
:sessionname MyNewName