Home >Operation and Maintenance >Linux Operation and Maintenance >How to use Screen to manage remote sessions?

How to use Screen to manage remote sessions?

青灯夜游
青灯夜游forward
2019-03-28 14:20:082349browse

The content of this article is to introduce the method of using Screen to manage remote sessions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

When developing locally, I often need to use remote connections to connect to Linux servers. At the beginning, I opened several SSH windows as long as I had several remotes. This method is very original and direct, but it is always affected by the problem. Password input is enough. Even if you use SSH to log in without a password, you will feel that entering SSH commands every time is not fast at all. Moreover, if you open too many windows, it will be confusing and difficult to manage. In this case, the tools on Linux There are many, let’s learn about the screen tool today.

When introducing the screen tool, let us first discuss why after connecting to the server, when executing a long-term task, we close the remote connection window and the task is killed?

The culprit: SIGUP signal

In Linux/Unix, there are the following concepts:

  • Process group: a collection of one or more processes, each process group has a unique process group ID;

  • session: one or A collection of multiple process groups has a unique session leader, and the session ID is the leader process ID;

  • The session can have a separate session terminal, which is the same as the control terminal. The first process of the connected session is called the controlling process, the process currently interacting with the terminal is called the foreground process group, and the remaining process groups are called the background process group.

Tool POSIX.1 definition:

  • The default action of the hang-up signal (SIGHUP) is to terminate the program;

  • When the terminal interface detects that the network connection is disconnected, it will send a SIGHUP signal to the control process (the first process of the session);

  • If the first process of the session terminates, this signal is sent To the foreground process group of the session;

  • When a process exits and an orphan process group is generated, if an orphan process group process is in the STOP state, SIGHUP and SIGCONT signals are sent to the process group. All processes in the process group.

Therefore, when the network is disconnected or the window is closed, the control process receives the SIGHUP signal and exits, which will cause other processes in the session to exit.

A simple example

Open two SSH terminal windows and execute the ping command in the first one:

How to use Screen to manage remote sessions?

In another terminal window, find the process ID of ping as 22132, and its parent process ID is 21803, that is, log in to zsh:

How to use Screen to manage remote sessions?

Using ps -xj, you can see that logging in to zsh (PID 21803) and ping are in the same session, zsh is the first process in the session, so the process group PGID is 21803 , the process group PGID of ping is 22132, which is the foreground process group.

How to use Screen to manage remote sessions?

At this time, we closed the first SSH remote window and saw that the ping process was killed in another window:

How to use Screen to manage remote sessions?

This shows that when we perform a time-consuming task, if we close the window, the task process will be killed, and it will not continue to execute in the background as we expected.

Using screen

Now, there is no need to say more about what screen does, let’s just talk about how to use it. Bar.

1. Directly execute the screen command

How to use Screen to manage remote sessions?

##screen will create a full-screen window for executing the shell. You can You can see that the title of the window indicates that it is in the screen program. In this window, you can execute commands at will. If you want to exit, enter the exit command.

2. If you want to open a new window to perform another task, you can execute:

// 方法1
按键盘Ctrl+a +c, 会心打开一个screen窗口

// 方法2
screen 需要执行的任务的命令,比如:
screen ping www.baidu.com
3. Multiple

screen window switching

方法1
键盘Ctrl+a +[0..9]

方法2
键盘Ctrl+a +n, 下一个screen窗口
键盘Ctrl+a +p, 上一个screen窗口
For more commands, see the picture below:

How to use Screen to manage remote sessions?

How to use Screen to manage remote sessions?

Screen is a window manager that can multiplex a physical terminal between multiple processes. Screen also has the concept of session. Users can create multiple screen windows in a screen session, and in each The screen window is like operating a real SSH connection window.

Recommended video tutorials: "Linux Tutorial"

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use Screen to manage remote sessions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete