The Linux screen command can be a life-saver when you need to ensure long-running tasks don't get killed when an SSH session is interrupted. Here's how to use it. Credit: Sandra Henry-Stocker If you’ve ever had to restart a time-consuming process because your SSH session was disconnected, you might be very happy to learn about an interesting tool that you can use to avoid this problem – the screen tool. Screen, which is a terminal multiplexor, allows you to run many terminal sessions within a single ssh session, detaching from them and reattaching them as needed. The process for doing this is surprising simple and involves only a handful of commands. To start a screen session, you simply type screen within your ssh session. You then start your long-running process, type Ctrl+A Ctrl+D to detach from the session and screen -r to reattach when the time is right. If you’re going to run more than one screen session, a better option is to give each session a meaningful name that will help you remember what task is being handled in it. Using this approach, you would name each session when you start it by using a command like this: $ screen -S slow-build Once you have multiple sessions running, reattaching to one then requires that you pick it from the list. In the commands below, we list the currently running sessions before reattaching one of them. Notice that initially both sessions are marked as being detached. $ screen -ls There are screens on: 6617.check-backups (09/26/2019 04:35:30 PM) (Detached) 1946.slow-build (09/26/2019 02:51:50 PM) (Detached) 2 Sockets in /run/screen/S-shs Reattaching to the session then requires that you supply the assigned name. For example: $ screen -r slow-build The process you left running should have continued processing while it was detached and you were doing some other work. If you ask about your screen sessions while using one of them, you should see that the session you’re currently reattached to is once again “attached.” $ screen -ls There are screens on: 6617.check-backups (09/26/2019 04:35:30 PM) (Attached) 1946.slow-build (09/26/2019 02:51:50 PM) (Detached) 2 Sockets in /run/screen/S-shs. You can ask what version of screen you’re running with the -version option. $ screen -version Screen version 4.06.02 (GNU) 23-Oct-17 Installing screen If “which screen” doesn’t provide information on screen, it probably isn’t installed on your system. $ which screen /usr/bin/screen If you need to install it, one of the following commands is probably right for your system: sudo apt install screen sudo yum install screen The screen tool comes in handy whenever you need to run time-consuming processes that could be interrupted if your SSH session for any reason disconnects. And, as you’ve just seen, it’s very easy to use and manage. Here’s a recap of the commands used above: screen -S start a session Ctrl+A Ctrl+D detach from a session screen -ls list sessions screen -r reattach a session While there is more to know about screen, including additional ways that you can maneuver between screen sessions, this should get you started using this handy tool. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe