Introduction to GNU Screen
If you do not know GNU Screen and you need to run a command in a terminal and then disconnect from a server before it is finished, this post is for you.
The online user's manual is certainly complete and comprehensive, but it is also very long to read. You can find it here: gnu.org/software/screen/manual.
In this post I'm just going to explain (in a way that's probably neither precise nor appreciated by purists) that GNU Screen allows you to manage virtual terminals, or sessions. For basic usage, you just need to know four commands:
# create a new session screen -S my_session_name # detach from the current session Ctrl+a Ctrl+d # list all sessions screen -ls # reattach to a session screen -r my_session_name
Now, let's see it in action: log in a remote server and run
> screen -S long_backup
This will temporarily hide the current terminal and open a new session from which you can, for example, run a very long backup:
> tar cfvz ~/mybackup.tgz /opt/bigdata
Now, while the command is running, press Ctrl+a and then Ctrl+d. The previous terminal is restored and a message appears about the session you detached from. Now you can list the running sessions in background:
[detached from 2490.long_backup] > screen -ls There is a screen on: 2490.long_backup (your timestamp here) (Detached) 1 Socket in /run/screen/S-username.
When you want to reconnect to the session in background, just use
screen -r long_backup
Remember, these four commands are the very minimum you can start with, but GNU Screen has a lot of other useful commands and options and you should read the manual to discover all the potential it offers.
Posted on 2023-10-24
Copyright © 2019-2023 Marcello Zaniboni