Article Title: Use screen to manage your remote sessions. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
I have encountered frequent disconnection issues recently, so it is really convenient to log on to the development environment and use screen through a jump machine. The work scenario can be retained. if it is disconnected, it will be no problem to use screen again next time, and the original work scenario will be retained.
--------------------------------------------------------------------------------
Do you often need to log on to the Linux server remotely? Do you often have headaches for long-running tasks? Are you still using nohup? Let's take a look at screen. it will surprise you!
Do you often need SSH or telent to remotely log on to the Linux server? Do you often have a headache for long-running tasks, such as system backup and ftp transmission. Generally, we open a remote terminal window for each such task because it takes too long to execute. You must wait for the task to complete. during this period, you cannot close the window or disconnect the connection. Otherwise, the task will be killed and everything will be abandoned.
Culprit: SIGHUP signal
Let's see why closing the window or disconnecting will cause the running program to die.
Linux/Unix has the following concepts:
Process group: a collection of one or more processes. each process group has a unique process group ID, that is, the ID of the process Leader process.
Session: a set of one or more process groups. it has a unique session leader ). The ID of the first process.
A single control terminal (controlling terminal) is available during the session ). The first process connecting to the control terminal is called the control process ). The process currently interacts with the terminal is called the foreground process Group. Other process groups are called background process groups.
According to POSIX.1:
The default action of SIGHUP is to terminate the program.
When the terminal interface detects that the network connection is disconnected, it sends the hanging signal to the control process (the first process in the session ).
If the first process of the session ends, the signal is sent to the foreground process Group of the session.
When a process exits and an orphan process group is generated, if any process in the orphan process Group is in the STOP state, the SIGHUP and SIGCONT signals are sent to all processes in the process Group.
Therefore, when the network is disconnected or the terminal window is closed, the control process receives the SIGHUP signal and exits, causing other processes to exit during the session period.
Let's look at an example. Open two SSH terminal windows and run one of the top commands.
[Root @ tivf09 root] # top
In another terminal window, find the top process ID is 5180, and its parent process ID is 5128, that is, log on to shell.
[Root @ tivf09 root] # ps-ef | grep toproot 5180 5128 0 000:00:02 pts/0 00:00:00 toproot 5857 3672 0 0pts/2 grep top
Use the pstree command to see the relationship more clearly:
[Root @ tivf09 root] # pstree-H 5180 | grep top |-sshd-+-sshd --- bash --- top
Run the ps-xj command. you can see that the login shell (PID 5128) and top are in the same session period. shell is the first process in the session period and the PGID of the process Group is 5128, the PGID of the top Process Group is 5180, which is the foreground process Group.
[Root @ tivf09 root] # ps-xj | grep 5128 5126 5128 5128 pts/0 5128 S 0 0: 00-bash 5180 5128 5180 5180 pts/0 5128 S 0 0: 50 top 5180 3672 18094 3672 pts/2 18094 S 0 grep 5128
Close the first SSH window. in the other window, you can see that the top is also killed.
[Root @ tivf09 root] # ps-ef | grep 5128 root 18699 3672 0 00:00:00 pts/2 grep 5128
If we can ignore the SIGHUP signal, closing the window will not affect the running of the program. The nohup command can achieve this purpose. if the program's standard output/standard error is a terminal, nohup redirects it to the nohup. out file by default. It is worth noting that the nohup command only allows the program to ignore the SIGHUP signal and also needs to be marked and run in the background.
Nohup[Argument…] &
Although nohup is easy to use, it is still relatively "simple". it can handle simple commands and is troublesome for complicated tasks that require human-computer interaction.
In fact, we can use a more powerful utility screen. Popular Linux distributions (such as Red Hat Enterprise Linux 4) usually come with screen utilities. if not, you can download them from the GNU screen official website.
[Root @ tivf06 ~] # Rpm-qa | grep screenxscreensaver-4.18-5.rhel4.11screen-4.0.2-5
[1] [2] [3] [4] Next page