Table of contents of the article:
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.