How to use Screen in Linux
You may know the following situation: You are working on a remote machine via SSH and perform a long running-task, then suddenly the connection drops and all your work is lost. Or you want to work on multiple things remotly without starting a new terminal and SSH into the machine again. Well for both of these scenarios there is a solution. GNU screen.
What is Screen
Screen is a so called terminal multiplexer. That means, you can start a terminal session and then open virtual terminals inside that session. This allows you to keep programs run in the background, while opening a new virtual terminal. Processes that a running inside a session will keep alive, even if their window is not visiable or the connections to the remote host drops.
NOTE Another quite popular used terminal multiplexer is
tmux
. If you want to learn more abouttmux
and its difference to GNU/Screen you can check out our tutorial here.
Installing Screen on Linux
In most modern distributions GNU Screen is already preinstalled. You can check if screen is already installed on your system with:
screen --version
The output should look like this:
root@linuxandi:~# screen --version
Screen version 4.09.00 (GNU) 30-Jan-22
If you don’t have Screen already installed on your system you can install it with the package manager of your distribution.
Install Screen on Ubuntu or Debian
Make sure you update your package list first and then install GNU Screen with:
sudo apt update && sudo apt install screen
Install GNU Screen on CenOS or Fedora
On CenOS or Fedora distros you can install Screen with the following command.
sudo yum install screen
After you made sure that Screen is succesfully installed, you can start using Screen.
Starting a Screen session
Starting a Screen session is relatively easy, simply type ‘screen’ into your terminal:
screen
This will open a new screen session. You will be greeted with the default screen window. Press [Space] or [ENTER] to continue to the shell.
If you want to get a list of available commands you can type:
‘Ctrl+a ?’ (Press Ctl and the ‘a’ key on the same time then type ?)
To give the session a name you can type ‘Ctr +a’ + ‘:’. Now you will see a prompt on the bottom, type sessionname name confirm it by pressing the ‘Enter’ Key.
To start a new named Session directly you can add the -S Parameter to the screen command. This can be useful if you using more then one session on the remote machine. So you can easily switch between the session.
screen -S SESSION_NAME
To exit the screen session we can either detach the session with ‘Ctr + a’ + ’d’ or type in exit to terminate the screen session.
Running a process in the Background
Screen is very useful when you have a program or task that needs to run in the background, while you want to do other things simultaneously. This can be used to run system updates or something like gamesservers that would otherwise stop if the terminal session disconnects.
To run a process in the background we will be using a simple Shell script for demonstration purposes.
First, we will create a new file “task.sh”.
#!/bin/bash
while true
do
echo "Long task..."
sleep 1
done
This simple Script outputs “Long task…” to the screen and will run until being terminated. If we would start the script normally, the terminal output would be blocked and we could not execute other commands in this terminal session. When we are running the script inside a screen session we can simply detach the screen session and let the script run in the background.
Then we need to mark the file as executable.
chmod +x
After that we can run the script with:
./task.sh
Now you can see that the terminal has been “blocked” and we cannot run any other commands. If we want to run another command, we need to either start a new terminal session or terminate the current running script, which in this cases we do not want, as we want to let the script run in the background.
To run the script in a screen session we need to do the following steps:
Step 1) Starting a new screen session
screen -S test-task
Here we started a new screen session with the name test-task.
Step 2) Run the script
Next we will run the script as usual, but now within the screen session.
./task.sh
The output will be the same as before.
Step 3) Detach the screen session
However now we can detach the screen session by typing: ‘Ctr + a’ + d.
Now we are back to our initial terminal session and we can continue doing stuff or even close the terminal and the task.sh will still run in the background.
Step 4) Verify that the process runs in the background
To verify the script is still running in the background, we can grep the process id for the task.sh script.
ps aux | grep task
And we will indeed see that the script is still running.
Linux Screen Cheatcheat
To create a new Window within a screen session press Ctr+a
c
. You can then switch between windows in a screen session with Ctr+a
+ 0..9
. You can see the corresponding number on the bottom of the Screen Session, next to the window name.
Here you will find some useful commands for managing Windows in Screen:
Command | Description |
---|---|
Ctrl+a c |
This will create a new Shell window |
Ctrl+a " |
Get a list of all active windows |
Ctrl+a A |
Rename the current active window |
Ctrl+a S |
Split Horizontally |
Ctrl+a “|” |
Split vertically |
Ctr+a tab |
Switch focus of panel to the next |
Ctrl+a Ctrl+a |
Toggle between next and previous panel |
Ctrl+a Q |
Close all panel but active one |
Ctrl+a X |
Close the current panel |
Ctrl+a d |
Detach the session |
Detach and reattach Sessions
You can detach a screen session by typing:
Ctrl+a
d
When detaching a session the programs in all windows of the sessions are still running and will continue running, even after you close your terminal window.
To reattach a session you can use the following command:
screen -r
If you are running multiple sessions you have to make sure you attach the right session ID after the -r
parameter.
To find the sessionID you can type screen -ls
to get a list of all currently running screen sessions.
For instance if you want to reattach the session with the id 10377.pts-0, type the following command:
screen -r 10377
to reattach to the screen session.
Customize GNU Screen
You can modify the Keybindings as well as appearances in the screenrc
config file. When screen
starts it will read the configuration either from /etc/screenrc
or from ~/.screenrc
if present. So you can customize the screen config file for each user.
Some useful Screenrc Configurations are:
# Turn off the welcome message
startup_message off
This can be used to turn of the Welcome Message. Also known as Message of the Day or MOTD
for short.
# Set scrollback buffer to 10000
defscrollback 10000
This option will limit the scrollbuffer to 10000 lines.
# NEXT WINDOW
# Ctrl + Alt + Right from gnome-terminal
bindkey ^[[1;7D next
You can even bind keys to certain functionality as for example binding Ctrl
+Alt
+Right
to go to the next active window. With the bindkey
option you can even overwrite default keybindings.
# Customize the status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
This line above changes the look of the status bar. You can even use colors and much more. To learn more about customizing Screen please refer to the Screen Manual
Conclusion
As you can see, Screen is very easy and straighforward to use. You only need to memorize some basic commands but you will be much more efficient, when including screen
into your daily workflow. You can create multiple terminal sessions and run multiple programs and commands in the background. You also learned how to customize and personalize your screen terminal by editing the .screenrc
configuration file.
If you want to learn more about GNU Screen in general, please refer to Screens User Manual. As always if you have questions or any feedback, feel free to use the comment section down below.