Linux Screen Tutorial
Screen is a shell layer that allows you to use one shell session, detach from it, and then reattach from a different computer where you left off. It’s useful for long jobs, recording what commands were run, and bad connections that might drop your SSH shell in the middle of a work session.
$ which screen
/usr/bin/screen
Install Screen
For Debian-Based Linux, the command is:
$ sudo apt-get install screen
Start Screen
Start screen with the simple “screen” command and hit enter to begin using it.
Screen Commands
Here is the TLDR version that you probably are looking for:
-
screen -S something
-
screen -ls
-
screen -r xxxx.something -D
Everything Else besides TLDR:
Linux Screen uses ctrl + a to start commands and then a character to do a specific command. You can see all of the commands using ctrl-a ?
Ctrl-a ? | Show Help File |
Ctrl-a H | Create or Stop Log of Session |
A pwd will tell you what your working directory is, and you will have a screenlog.0 file in it with whatever commands you typed. If you Ctrl-a H again it will stop logging.
Ctrl-a M | Monitor for output |
Ctrl-a _ | Monitor for silence |
Ctrl-a x | Lock Screen |
Ctrl-a d | Detach Screen |
Ctrl-a c | Create new screen |
screen -ls | List all Screens |
screen -r xxxx | Reattach to existing Screen xxxx |
Ctrl-a n | Cycle to Next Screen |
Ctrl-a p | Cycle to Previous Screen |
Ctrl-a k | Stop Screen |
When you finally type “exit” or ctrl-a k to kill a session you will see the [screen is terminating] message.
Check if Screen is Running
If screen isn’t running, you can verify using the pgrep utilitiy
$ pgrep screen
If screen is running, you will get 2 process IDs:
$ pgrep screen
13020
13021
Example of Using Screen
screen | Start screen |
ctrl a, H | Turn on logging |
ctrl a, c | Make a New Screen |
start some command | long boring output |
ctrl a, c | Makes a new screen |
do other commands | bleh bleh whatever |
ctrl a, n | Cycle to the next screen |
exit | Type exit, it will scroll through screens shutting them down each time. |
Now you can review screenlog.0 file