Many times we need to perform a long-running task on a remote machine. The SSH connection might get disconnected due to a network connectivity issue or power failure. As a result of that, you will lose all your work as you can’t resume the session. Luckily, Linux provides a utility called screen that allows us to resume sessions. In this article, I will be discussing using Screen in Linux.
What is Screen in Linux?
Screen or GNU Screen is a full-screen terminal multiplexer. In other words, you can start a screen session, perform any task on any number of windows inside that session. You can detach from the screen session and reattach to it anytime. Processes running in a screen session in Linux will continue to run even if you get disconnected. This allows you to run a long-running task without worrying about keeping your SSH connection active. Moreover, you can share your screen session with other users by attaching/detaching terminal sessions.
Installing Linux Screen
screen -v
Output:
Screen version 4.06.02 (GNU) 23-Oct-17
If you do not have Screen installed, then you may install it from your package manager.
Installing Screen on Ubuntu or Debian
sudo apt-get install screen
Installing Screen on Fedora or CentOS
sudo yum install screen
Starting Screen in Linux
To start a screen in Linux, simply type in the following command:
screen
This will open a new screen session. You can get a list of commands by typing in the following keys on your keyboard:
Ctrl+a ?
Detaching Linux Screen
One of the best advantages of using Screen in Linux is that you can detach from it anytime. The detached screen will continue to work in the background even if you disconnect the SSH session.
To detach from Screen in Linux, use the following keys on your keyboard:
Ctrl+a d
This can come handy when you have to perform a long-running task. For example, you want to run a python script to perform some complex calculations on a remote server. The script is expected to take 2 hours to complete. You don’t need to constantly keep SSH connection active for the script to run. You can simply enter a screen session, start the script and detach from it. The script will continue to run in the background on the remote server.
Resume Screen
In order to resume a Linux screen, you can type in the following command:
screen -r
If you only have one screen running currently, then that screen will be resumed. If you have multiple screens running then it will display the list of the running screen.
screen -r
There are several suitable screens on:
19830.pts-0.vishesh-HP-Concatly (Sunday 27 January 2019 12:43:44 IST) (Detached)
19809.pts-0.vishesh-HP-Concatly (Sunday 27 January 2019 12:43:39 IST) (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
To resume a screen from the list, use the following command:
screen -r [processid]
screen -r 19830.pts-0.vishesh-HP-Concatly
Listing Screen
Additionally, you can also list the available screens using the below command:
screen -ls
There are screens on:
19830.pts-0.vishesh-HP-Concatly (Sunday 27 January 2019 12:43:44 IST) (Detached)
19809.pts-0.vishesh-HP- Concatly (Sunday 27 January 2019 12:43:39 IST) (Detached)
2 Sockets in /run/screen/S-vishesh.
Starting Named Screen
By default, the screen starts a new screen session with a random id. You can start a named screen session as it is much easier to remember.
screen -S name_of_screen
screen -S concatly
screen -ls
There are screens on:
20154.concatly (Sunday 27 January 2019 12:52:13 IST) (Detached)
19830.pts-0.vishesh-HP-Concatly (Sunday 27 January 2019 12:43:44 IST) (Detached)
19809.pts-0.vishesh-HP-Concatly (Sunday 27 January 2019 12:43:39 IST) (Detached)
3 Sockets in /run/screen/S-vishesh.
Terminating Screen
You can terminate a screen session by using the exit command in your screen. If you terminate a screen then you won’t be able to resume it again.
Practical Usage
Let’s see a practical usage of by using a sample script.
Step 1
Create the below PHP script to output something on the terminal in an infinite loop:
<?php $counter = 1; while(true) { echo "Script Running: ".$counter.PHP_EOL; $counter++; sleep(2); } ?>
Step 2
Enter a screen using screen -S command.
screen -S testScreen
Step 3
Start the PHP script in the screen session. It will print something on the terminal every 2 seconds.
php screen.php
Script Running: 1
Script Running: 2
Script Running: 3
Script Running: 4
Script Running: 5
Step 4
Detach from the screen.
Ctrl+A d
Step 5
Resume the screen.
screen -r
You will notice that the script was still running in the screen session even when we detached from it.
Conclusion
In this article, you learned about starting multiple screen sessions and resuming them on Linux. You can also read more articles on Linux on Concatly!
There is a lot more to learn about GNU Screen at Screen’s User Manual.

Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.