July 4 2023

The Magic of Multitasking with Screen on Linux: An Essential Guide

Find out how to improve your efficiency with the use of Screen, the tool for managing terminal sessions on Linux.

Linux Screen

In the world of Linux system administration, tools that allow you to multitask and multiplex terminal sessions are crucial. These tools facilitate the execution of background processes, allowing users to free terminal resources for other operations, without interrupting the running process. Among the various tools available, “Screen” is one of the most popular and effective. This post will explore using Screen in a Linux environment.

Advantages of running a command in the background

Before we delve into how Screen works, it's important to understand why you might want to run a command in the background. A long and demanding operation can tie up the terminal for a considerable time, preventing the execution of other commands. Instead of waiting for the process to finish, you can run it in the background, freeing up the terminal for other tasks.

Another benefit of running commands in the background is that processes can continue to run even after the user logs off. This is especially useful in a network environment, where a remote user may want to start a process on a server and leave it running regardless of their connection.

What is Screen

Screen is a full-screen terminal session manager for Unix-like operating systems. It allows the user to start a terminal session, run an application, and then detach and reattach the session, even from a remote connection. Screen is very useful when working on long sessions that may need to be resumed at a later time or when working on remote sessions where the connection may be dropped.

How to launch a background command with Screen

Using Screen is pretty straightforward. To start a new session, just type "screen" in the command line. Once the session is started, you can run any command or application as you would in a normal terminal. For example, you could start a complex and lengthy backup process.

To disconnect the session and continue it in the background, use the key combination 'Ctrl-A' followed by 'D'. The screen will revert to the original shell and the process will start running in the background.

How to recover a screen session

One of Screen's most powerful features is the ability to reattach a previous session. To do this, you must first locate the ID of the session you want to retrieve. This can be done with the "screen -ls" command, which will list all active sessions.

Once the ID of the session has been identified, it can be reattached with the command “screen -r [ID]”. This will return the user to the Screen session exactly as they left off, allowing them to continue where they left off.

How to detach to continue the session in the background

As we've already mentioned, one of Screen's most distinctive and useful features is its ability to "detach" a running terminal session and leave it running in the background. This allows the user to free up the command line for other tasks, without interrupting already started processes.

To activate this function, you must be in a Screen session and use the key combination 'Ctrl-A' followed by 'D'. This command will immediately detach the active terminal session, putting it in the background and returning control to the main terminal. From here, the user can start other Screen sessions, execute other commands, or even exit the terminal or log out, while the detached session continues to function regardless.

What makes this feature particularly powerful is that, once detached, the Screen session is not tied to the user's terminal or connection. It continues to run in the background, even if the user logs out or closes the terminal. This is particularly useful in the case of long-running processes or operations on remote servers. For example, you could start a long backup process on a remote server, detach the Screen session, and then log out. The backup process will continue to run on the server regardless of your connection.

This kind of independence allows you to flexibly manage processes and tasks, providing seamless control and effective use of resources. Whether you are a system administrator working on remote servers, or a user performing time consuming tasks, Screen offers an easy and efficient way to manage your tasks.

Sometimes it may happen that the combination CTRL + A does not seem to give any signal or produce any change, in reality you will only have to press D afterwards to see the detach message.

Commands, Options, Arguments and Shortcuts:

Syntax:

screen [-opts] [cmd [args]]

Options:

-a: Force all capabilities in each termcap of the window.
-A -[r|R]: Fit all windows to the new display width and height.
-c file: Read the configuration file instead of '.screenrc'.
-d (-r): Detach the running screen elsewhere (and reattach it here).
-dmS name: Start as daemon: Screen session in detached mode.
-D (-r): Remote detach and hang up (and hang up here).
-D -RR: Do what is necessary to get a screen session.
-e xy: Change command characters.
-f: Turn on flow control, -fn = off, -fa = auto.
-h lines: Set the size of the scroll back history buffer.
-i: Stop output earlier when flow control is active.
-l: Turn on access mode (update /var/run/utmp), -ln = off.
-ls [match]: Show all linked screens.
-L: Enable output logging.
-m: Ignore the $STY variable, create a new screen session.
-O: Choose optimal output rather than exact vt100 emulation.
-p window: Preselect the named window if it exists.
-q: Silent startup. Exit with non-zero return code if failed.
-Q: Commands will send response to stdout of query process.
-r [session]: Reattach to a detached screen process.
-R: Hang up if possible, otherwise start a new session.
-S sockname: Name this session .sockname instead of …
-t title: Set the title. (window name).
-T term: Use term as $TERM for windows, rather than "screen".
-U: Tell screen to use UTF-8 encoding.
-v: Print “Screen version 4.06.02 (GNU) 23-Oct-17”.
-x: Link to a non-detached screen. (Multiple display mode).
-X: Run as screen command in the specified session.

Hotkey options:

Ctrl-a + c: Create a new window.
Ctrl-a + w: List all currently open windows.
Ctrl-a + A: Rename the current window. The name will appear when you list open windows with Ctrl-a + w.
Ctrl-a + n: Go to next window.
Ctrl-a + p: Go to previous window.
Ctrl-a + Ctrl-a: Go back to the last used window.
Ctrl-a + k: Close the current window (kill).
Ctrl-a + S: Split the current window horizontally. To switch between windows, do Ctrl-a + Tab.
Ctrl-a + |: Split the current window vertically.
Ctrl-a + d: Detach a screen session without stopping it.

Conclusion

Screen, as a terminal session manager, is a powerful ally for anyone working with Linux systems. Its versatility is reflected in the numerous functions it offers, making it a real multitool in the field of system administration.

The first and obvious advantage of Screen is its ability to free up system resources for other tasks. This allows users to engage in multiple tasks simultaneously, without having to wait for a command or process to complete its execution cycle. In practice, Screen allows you to delegate a series of operations to the operating system, and then return to check their status or their results at a later time.

But Screen doesn't stop there. It also offers the ability to manage long-running processes that can be suspended and resumed later. This is extremely useful for operations that take a long time to complete, such as backing up large amounts of data, running complex maintenance scripts, or implementing large-scale software updates. With Screen, these processes can be started, monitored and, if necessary, suspended, then resumed exactly where they left off, without losing any progress.

For those who work in remote environments, Screen offers an extra level of convenience. The ability to disconnect a session in progress, disconnect from the system and then reconnect the same session at a later time, regardless of geographical location, makes Screen an indispensable tool for remote administration of servers and systems.

In conclusion, for those who manage complex and time-consuming operations, work remotely, or simply want to optimize their workflow with the terminal, Screen can be an invaluable addition to your toolbox. With its wide range of features and ease of use, Screen proves to be a trusted ally in system administration, making life for Linux users not only easier, but more productive as well.

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION

Managed Server Srl is a leading Italian player in providing advanced GNU/Linux system solutions oriented towards high performance. With a low-cost and predictable subscription model, we ensure that our customers have access to advanced technologies in hosting, dedicated servers and cloud services. In addition to this, we offer systems consultancy on Linux systems and specialized maintenance in DBMS, IT Security, Cloud and much more. We stand out for our expertise in hosting leading Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Joomla, OpenCart and Magento, supported by a high-level support and consultancy service suitable for Public Administration, SMEs and any size.

Red Hat, Inc. owns the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. owns the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt. Oracle Corporation owns the rights to Oracle®, MySQL®, and MyRocks®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; REDIS® is a registered trademark of Redis Labs Ltd. F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB. Adobe Inc. holds the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited. Automattic Inc. owns the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert holds the rights to Drupal®. Amazon Web Services, Inc. holds the rights to AWS®; Google LLC holds the rights to Google Cloud™ and Chrome™; Microsoft Corporation holds the rights to Microsoft®, Azure®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®. Apache® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group. CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV Hetzner Online GmbH owns the rights to Hetzner®; OVHcloud is a registered trademark of OVH Groupe SAS; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Facebook, Inc. owns the rights to Facebook®. This site is not affiliated, sponsored or otherwise associated with any of the entities mentioned above and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. Any other trademarks mentioned belong to their registrants. MANAGED SERVER® is a trademark registered at European level by MANAGED SERVER SRL, Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

Back to top