screen command Example: manage multiple terminal sessions | Linux China

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/91449135
640?wx_fmt=jpeg As the name suggests, Screen may be multiplexed between a plurality of physical terminal interactive shell, so we can perform different tasks at each terminal session. All sessions are completely independent of the Screen run the program. - Sk

GNU Screen is a terminal multiplexer (window manager). As the name suggests, Screen may be multiplexed between a plurality of physical terminal interactive shell, so we can perform different tasks at each terminal session. All sessions are completely independent of the Screen run the program. Therefore, even if the session closed unexpectedly or disconnected, running in the Screen session or process will continue to run. For example, when using SSH upgrade Ubuntu  server, the screen command will continue to run the upgrade process, just in case SSH session is terminated for any reason.

GNU Screen allows us to easily create multiple Screen session and switch between different sessions, copy text between sessions, at any time on or even out of the session and so on. It is one of the important commands for each Linux administrators should learn and use when necessary line tool. In this brief guide, we will see the  screen basic usage of commands and examples in Linux.

Install GNU Screen

GNU Screen in most Linux operating system's default repository are available.

To install GNU Screen on Arch Linux, run:

 
  
$ sudo pacman -S screensudo pacman -S screen

In Debian, Ubuntu, Linux Mint on:

 
  
$ sudo apt-get install screensudo apt-get install screen

On Fedora:

 
  
$ sudo dnf install screensudo dnf install screen

On RHEL, CentOS:

 
  
$ sudo yum install screensudo yum install screen

On SUSE / openSUSE:

 
  
$ sudo zypper install screensudo zypper install screen

Let's continue with the  screen example command.

A plurality of terminal session management command examples Screen

In the Screen default prefix shortcuts for all commands is  Ctrl + a. When you use Screen, you often need to use this shortcut. So, remember this keyboard shortcut.

Creating a new Screen Session

Let's create a new Screen session and connect to it. To do this, type the following command in the terminal:

 
  
screen

Now, run any program or process in this session, even if you disconnect from this session, process or running program will continue to run.

Released from the Screen session

Out from the screen session, press  Ctrl + a and  d. You do not need to press the two key combination. First press  Ctrl + a and then press  d. From the conversation off, you will see something like the following output.

 
  
[detached from 29149.pts-0.sk]detached from 29149.pts-0.sk]

Here 29149 is the Screen ID, pts-0.sk is the name of a screen session. You can use the Screen ID or session name corresponding to the company, out of the screen and terminates the session.

Create a named session

You can also create a Screen session with any custom name of your choice, instead of the default user name, as shown below.

 
  
screen -S ostechnix -S ostechnix

The above command will create a file named  xxxxx.ostechnix new Screen session and immediately connect to it. Out from the current session, press  Ctrl + aand press  d.

When you want to find out what processes are running on which sessions, naming the session would be useful. For example, when the system is disposed in the session LAMP, you can simply be named as follows.

 
  
screen -S lampstack -S lampstack

Created out of session

Sometimes, you may want to create a conversation, but do not want to automatically connect to the session. In this case, run the following command to create a file named senthil has been out of the session:

 
  
screen -S senthil -d -m -S senthil -d -m

It can be shortened to:

 
  
screen -dmS senthil -dmS senthil

The above command will create a named  senthil session, but will not connect to it.

Lists the screen session

To list all running sessions (or even on the off), run:

 
  
screen -ls -ls

Sample output:

 
  
There are screens on: are screens on:
    29700.senthil   (Detached)29700.senthil   (Detached)
    29415.ostechnix (Detached)29415.ostechnix (Detached)
    29149.pts-0.sk  (Detached)29149.pts-0.sk  (Detached)
3 Sockets in /run/screens/S-sk. Sockets in /run/screens/S-sk.

As you can see, I have three running sessions and all sessions are off.

Even on Screen Session

If you want to connect to the session, for example  29415.ostechnix, simply run:

 
  
screen -r 29415.ostechnix -r 29415.ostechnix

or:

 
  
screen -r ostechnix -r ostechnix

Or Screen ID:

 
  
screen -r 29415 -r 29415

To verify that we connect to the above-mentioned session, just list the open session and check.

 
  
screen -ls -ls

Sample output:

 
  
There are screens on: are screens on:
        29700.senthil   (Detached)29700.senthil   (Detached)
        29415.ostechnix (Attached)29415.ostechnix (Attached)
        29149.pts-0.sk  (Detached)29149.pts-0.sk  (Detached)
3 Sockets in /run/screens/S-sk. Sockets in /run/screens/S-sk.

As you can see in the above output, we now connect to the  29415.ostechnix session. To exit the current session, press  ctrl + a d.

To create a nested conversation

When we run the  screen command, it creates a session for us. However, we can create nested session (a session within the session).

First, create a new session or even open session. Then I will create a file called  nested new session.

 
  
screen -S nested -S nested

Now, in the session press  Ctrl + a and  c create another session. Just repeat this operation can create any number of nested Screen session. Each session will be assigned a number. Number from the  0 start.

You can press  Ctrl + n to move to the next session, and then press  Ctrl + p to move to the previous session.

The following are important session management nested list of keyboard shortcuts.

◈  Ctrl + a "  - lists all the sessions ◈  Ctrl + a 0  - Switch to the session number 0◈  Ctrl + a n  - switches to the next session ◈  Ctrl + a p  - a session handover to ◈  Ctrl + a S  - current region is divided horizontally into two regions ◈  Ctrl + a l  - current region is divided vertically into two regions ◈  Ctrl + a Q  - close all sessions except the current session of the ◈  Ctrl + a X  - close the current session ◈  Ctrl + a \  - terminate all sessions and terminate Screen◈  Ctrl + a ?  - display key bindings. To exit, press the Enter Lock Session ####

Screen has an option to lock the session. To do this, press  Ctrl + a and  x. Enter your password to lock Linux.

 
  
Screen used by sk <sk> on ubuntuserver. used by sk <sk> on ubuntuserver.
Password::

Recording session

You may want to record all the contents Screen session. To do this, simply press  Ctrl + a and  H can be.

Or, you can use the  -L parameters to start a new session to enable logging.

 
  
screen -L -L

From now on, you do all the activities in the session will be recorded and stored in a  $HOME directory named  screenlog.xfile. Here x is a number.

You can use the  cat command or any text viewer to view the contents of the log file.

640?wx_fmt=png

Screen recording session

Screen session termination

If you no longer need to session, just kill it. To kill called  senthil disengagement session:

 
  
screen -r senthil -X quit -r senthil -X quit

or:

 
  
screen -X -S senthil quit -X -S senthil quit

or:

 
  
screen -X -S 29415 quit -X -S 29415 quit

If there is no open session, you will see the following output:

 
  
$ screen -lsscreen -ls
No Sockets found in /run/screens/S-sk. Sockets found in /run/screens/S-sk.

For more details, please refer to the man page:

 
  
$ man screenman screen

There is also a similar command line utility called Tmux, it performs the same work with GNU Screen. For more information, please refer to the following guidelines.

◈  tmux command examples: managing a plurality of terminal sessions

Resources

◈  the GNU Screen Home

via: https://www.ostechnix.com/screen-command-examples-to-manage-multiple-terminal-sessions/

Author: SK  topics: lujun9972  Translator: wxy  proofread: wxy

This article from the  LCTT  original compiler, Linux China  is proud

640?wx_fmt=jpeg


Guess you like

Origin blog.csdn.net/F8qG7f9YD02Pe/article/details/91449135
Recommended