Linux Administrator

How to Use Screen Command in Linux/UNIX

Screen command keep running terminal session alive in the background which can be detached and reconnect again. Screen is a Linux tool that is used to multiplexes a physical console between several process. This is especially useful when you log in to the Linux system remotely. You can start a screen, kick off a command, detach from the screen, and log out. You can then log in later and reattach to the screen and see the program running.

In this article I will show you can use screen command in Linux/UNIX system.

Find the below options are available with the screen command:

# screen --help
Use: screen [-opts] [cmd [args]]
 or: screen -r [host.tty]

Options:
-4            Use IPv4.
-6            Use IPv6.
-a            Force all capabilities into each window's termcap.
-A -[r|R]     Adapt all windows to the new display width & height.
-c file       Read configuration file instead of '.screenrc'.
-d (-r)       Detach the elsewhere running screen (and reattach here).
-dmS name     Start as daemon: Screen session in detached mode.
-D (-r)       Detach and logout remote (and reattach here).
-D -RR        Do whatever is needed to get a screen session.
-e xy         Change command characters.
-f            Flow control on, -fn = off, -fa = auto.
-h lines      Set the size of the scrollback history buffer.
-i            Interrupt output sooner when flow control is on.
-l            Login mode on (update /var/run/utmp), -ln = off.
-list         or -ls. Do nothing, just list our SockDir.
-L            Turn on output logging.
-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.
-r            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 .sockname instead of ...
-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.00.03 (FAU) 23-Oct-06".
-wipe         Do nothing, just clean up SockDir.
-x            Attach to a not detached screen. (Multi display mode).
-X            Execute  as a screen command in the specified session.

Start Screen Session

Follow the below command to start the screen session:

# screen

Now you can run the any command in screen session. If you execute the screen command a terminal launched in background.

Detach/Logout From the Screen Session

To detach and logout from the screen session press the Ctrl+a+d key in sequence. After pressing these keys you will get a message “detached” on the screen when session is detached.

Ctrl+a+d
[detached]

You can also use –d option if you want to logout and detach the screen from another terminal.

You will need to specify the screen ID which can be obtained from screen –ls command.

Syntax

screen -d [screen id]

Command example:

# screen -ls
There are screens on:
        49483.pts-0.node01       (Attached)
1 Socket in /var/run/screen/S-root.

# screen -d 49483.pts-0.node01
[49483.pts-0.node01 detached.]

Reattach to the Screen Session

Follow the below command to reattach the screen session.

# screen -r

List All Running Screen Session On The System

Follow the below command to list all running screen session with their status such as attached, detached on your system.

# screen -ls
There are screens on:
        49483.pts-0.node01       (Detached)
        20224.pts-0.node02       (Detached)
2 Sockets in /var/run/screen/S-root.
Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment