Install and Use Linux Screen Command with Best Examples

Posted on

Install and Use Linux Screen Command with Best Examples

Install and Use Linux Screen Command with Best Examples

In this guide, we’ll explore how to Install and Use Linux Screen Command with Best Examples. The screen command in Linux provides a powerful way to manage multiple shell sessions within a single SSH connection. It allows you to start processes within a screen session, detach from that session, and then reattach later, all while the processes continue to run in the background. This is incredibly useful for long-running tasks or when you need to disconnect from a remote server without interrupting your work.

When a process is initiated using screen, it operates within the screen’s managed environment. This means that even if you disconnect from the server, the process continues to execute. You can later reconnect to the session and find your terminal exactly as you left it.

This guide will walk you through the installation and usage of Screen on a Linux system.

To follow along, you’ll need:

  • A user account with sudo privileges.
  • Access to a command-line interface (terminal).

1. Install Screen Command in Linux

In many Linux distributions, the screen command comes pre-installed. To check if it’s already available on your system, run:

screen --version
Install Linux screen Command

If screen isn’t installed, you can install it using your distribution’s package manager.

For CentOS and RHEL systems:

sudo dnf install screen -y

For Debian and Ubuntu systems:

sudo apt install screen -y

2. Start Linux Screen Command

To begin a new screen session, simply type:

screen

This command will open a new window with a shell prompt within the screen session.

To see a list of available options, use the help command:

screen --help

The output displays various parameters and commands that can be used with screen:

**Output**
-i            Interrupt output sooner when flow control is on.
-l            Login mode on (update /var/run/utmp), -ln = off.
-ls [match]   or
-list         Do nothing, just list our SockDir [on possible matches].
-L            Turn on output logging.
-Logfile file Set logfile name.
-m            ignore $STY variable, do create a new screen session.
-O            Choose optimal output rather than exact vt100 emulation.
-p window     Preselect the named window if it exists.
-q            Quiet startup. Exits with non-zero return code if unsuccessful.
-Q            Commands will send the response to the stdout of the querying process.
-r [session]  Reattach to a detached screen process.
-R            Reattach if possible, otherwise start a new session.
-s shell      Shell to execute rather than $SHELL.
-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t title      Set title. (window's 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.08.00 (GNU) 05-Feb-20".
-wipe [match] Do nothing, just clean up SockDir [on possible matches].
-x            Attach to a not detached screen. (Multi display mode).
-X            Execute <cmd> as a screen command in the specified session.

3. Linux Screen Keystrokes

Once inside a screen session, you can use keystrokes to manage windows. These keystrokes begin with Ctrl + a, followed by another key.

Here’s a table of commonly used keystrokes:

**Keystrokes** **What They Do**
**Ctrl** **+** **a** and **c** Open a new screen window.
**Ctrl + a** and **”** List all open windows.
**Ctrl + a** and **0** Switch to window 0 (or any other numbered window).
**Ctrl + a** and **A** Rename the current window.
**Ctrl + a** and **S** Split the screen horizontally, with the current window on top.
**Ctrl + a** and **|** Split the screen vertically, with the current window on the left.
**Ctrl + a** and **tab** Switch focus between areas of the split screen.
**Ctrl + a** and **Ctrl + a** Switch between current and previous windows.
**Ctrl + a** and **n** Switch to the next window.
**Ctrl + a** and **p** Switch to the previous window.
**Ctrl + a** and **Q** Quit all other windows except the current one.
**Ctrl + a** and **X** Lock the current window.
**Ctrl + a** and **H** Create a running log of the session.
**Ctrl + a** and **M** Monitor a window for output (a notification pops up when that window has activity).
**Ctrl + a** and **_** Watch a window for the absence of output (such as when a file finishes downloading or a compiler finishes).

4. Named Sessions with Linux Screen

Naming your screen sessions is highly recommended for easier management, especially when running multiple sessions.

Use the following command to create a named session:

screen -S session_name

For example, to create a session named "upgrade", use:

screen -S upgrade

5. Detach Linux Screen Session

To detach from a screen session without terminating the running processes, use the following keystroke:

Ctrl+a d

6. Reattach the Linux screen Session

To reattach to a detached screen session, use:

screen -r

If you have multiple detached sessions, you can list them with:

screen -ls

The output will show the session IDs. To reattach to a specific session, use its ID:

screen -r session-id

7. Customize Screen Command

The screen command can be customized through configuration files. The system-wide configuration file is located at /etc/screenrc, and the user-specific configuration file is located at ~/.screenrc.

To edit the system-wide configuration file:

sudo vi /etc/screenrc

You can modify various settings by uncommenting lines (removing the # symbol).

Important Note: The screen package is deprecated and not included in RHEL8. Instead, you can use the tmux package.

Now you know how to Install and Use Linux Screen Command with Best Examples.

Conclusion

In this guide, you’ve learned how to Install and Use Linux Screen Command with Best Examples. The screen command provides a valuable tool for managing terminal sessions and running long-lasting processes in the background.

Alternative Solutions

While screen is a classic and widely used tool, other options offer similar functionality with potentially enhanced features or different approaches to terminal management. Here are two alternative solutions to consider:

1. Tmux

tmux (Terminal Multiplexer) is often considered a modern replacement for screen. It offers similar functionality, allowing you to create, manage, and detach/reattach terminal sessions. However, tmux provides several advantages, including:

  • More intuitive keybindings: tmux uses a slightly different, and often more user-friendly, set of keybindings.
  • Client-server architecture: tmux has a client-server architecture, making it more robust and allowing multiple clients to connect to the same session.
  • Extensibility: tmux is highly extensible through plugins and custom configurations.

Example Usage:

  1. Install Tmux:

    sudo apt install tmux  # Debian/Ubuntu
    sudo dnf install tmux  # CentOS/RHEL
  2. Start a new session:

    tmux new -s my_session
  3. Detach from the session: Ctrl + b, then d

  4. List sessions:

    tmux ls
  5. Attach to a session:

    tmux attach -t my_session

tmux is a great alternative to Install and Use Linux Screen Command with Best Examples.

2. Nohup and Backgrounding

While not a direct replacement for screen, using nohup in conjunction with backgrounding processes can achieve a similar outcome for simple use cases – specifically, running a single command or script that you want to continue executing even after you disconnect.

nohup prevents the process from receiving a hangup signal when the terminal closes, and & places the process in the background.

Example Usage:

nohup ./my_script.sh &

This command will run my_script.sh in the background, and its output will be redirected to a file named nohup.out. You can then disconnect from the server, and the script will continue to run.

Limitations:

  • No session management: Unlike screen or tmux, nohup doesn’t provide session management capabilities. You can’t reattach to the process or create multiple windows.
  • Single process: nohup is best suited for running a single command or script, not for managing multiple interactive sessions.

However, for simple tasks, nohup and backgrounding can be a lightweight alternative to screen. In conclusion, you have learned Install and Use Linux Screen Command with Best Examples, and explored alternative solutions for terminal session management.

Leave a Reply

Your email address will not be published. Required fields are marked *