We often encounter this problem. We use Telnet/ssh to log on to a remote Linux server and run some time-consuming tasks. As a result, the task fails midway through due to network instability. After the command is submitted, how does one prevent the local terminal window/network disconnection? Here are some examples. You can select different methods for different scenarios to solve this problem.
INohup/setsid /&
Scenario:
If a temporary command needs to be run for a long time, what method can be used to ensure its stable operation in the background?
Reason for hangup name
In earlier versions of UNIX, each terminal communicates with the system through modem. When a user logs out, the modem hangs up. Similarly, when the modem is disconnected, it will send an hangup signal to the terminal to notify it to close all sub-processes.
Solution:
We know that when a user logs out or the network is disconnected, the supervisor will receive an HUP signal to close all its sub-processes. Therefore, our solution has two ways: either let the process ignore the HUP signal, or let the process run in a new session to become a child process that does not belong to this terminal.
1. nohup
Nohup is undoubtedly the first method we have come up. As the name suggests, nohup is used to make the submitted command ignore The hangup signal. Let's take a look at the help information of nohup:
Nohup (1) user commands nohup (1) Name nohup-run a command immune to hangups, with output to a non-ttysynopsis nohup Command [Arg]... nohup optiondescription Run Command, ignoring hangup signals. -- help display this help and exit -- version output version information and exit
It can be seen that nohup is very convenient to use. You only need to add nohup before the command to be processed. The standard output and standard error will be redirected to the nohup. Out file by default.
Generally, we can add"&"To run commands in the background at the same time.">Filename2> & 1"
To change the default redirection file name.
Nohup example
[Root @ pvcent107 ~] # Nohup Ping www.ibm.com &
[1] 3059
Nohup: appending output to 'nohup. out'
[Root @ pvcent107 ~] # Ps-Ef | grep 3059
Root 30599840 00:00:00 pts/3 Ping www.ibm.com
Root 3067 984 0 00:00:00 pts/3 grep 3059
[Root @ pvcent107 ~] #
2 setsid
Nohup can undoubtedly avoid interruption of our process by ignoring the HUP signal. However, if we think from another perspective that our process is not a sub-process of the terminal that accepts the HUP signal, then it will naturally not be affected by the HUP signal. Setsid can help us do this. Let's take a look at the help information of setsid:
Setsid (8) Linux programmer's Manual setsid (8) Name setsid-run a program in a new sessionsynopsis setsid program [arg...] description setsid runs a program in a new session.
It can be seen that the use of setsid is also very convenient, you only need to add setsid before the command to be processed.
Setsid example
[Root @ pvcent107 ~] # Setsid Ping www.ibm.com [root @ pvcent107 ~] # Ps-Ef | grep www. IBM. comroot 3109410 07:28? 00:00:00 Ping www. IBM. comroot 31102 29217 0 00:00:00 pts/4 grep www.ibm.com [root @ pvcent107 ~] #
In the preceding example, the process ID (PID) is 31094, and its parent ID (ppid) is 1 (that is, the INIT process ID ), it is not the process ID of the current terminal. Compare this example with the parent ID in the nohup example.
3 &
Here is also a tip about subshell. We know that the inclusion of one or more names in "()" allows these commands to run in the sub-shell, thus extending many interesting functions, we will discuss one of them now.
After we put "&" into "()", we will find that the submitted jobs are not in the job list, that is, they cannot passJobs
. Let's take a look at why we can escape the influence of the HUP signal.
Subshell example
[Root @ pvcent107 ~] # (Ping www.ibm.com &) [root @ pvcent107 ~] # Ps-Ef | grep www. IBM. comroot 1627010 00:00:00 pts/4 00:00:00 Ping www. IBM. comroot 16278 15362 0 pts/4 grep www.ibm.com [root @ pvcent107 ~] #
From the above example, we can see that the parent ID (ppid) of the newly submitted process is 1 (pid of the INIT process) and is not the process ID of the current terminal. Therefore, it does not belong to the sub-process of the current terminal, so it will not be affected by the HUP signal of the current terminal.
IIDisown
Scenario:
We already know that adding nohup or setsid before the command can avoid the influence of the HUP signal. But if we have submitted the command without any processing, how can we remedy it to avoid the impact of the HUP signal?
Solution:
It is too late to add nohup or setsid. You can only solve this problem through job scheduling and disown. Let's take a look at the help information of disown:
Disown [-ar] [-H] [jobspec...] without options, each jobspec is removed from the table of active jobs. if the-H option is given, each jobspec is not removed from the table, but is marked so that sighup is not sent to the job if the shell has es a sighup. if no jobspec is present, and neither the-A nor the-r option is supplied, the current job is used. if no jobspec is supplied, the-A option means to remove or mark all jobs; the-r option without a jobspec argument restricts operation to running jobs. the return value is 0 unless a jobspec does not specify a valid job.
We can see that we can achieve our goal in the following ways.
Flexible Use of Ctrl-z
In our daily work, we can use ctrl-Z to suspend the current process to the background and execute some other operations, then, use FG to re-run the suspended process back to the foreground (you can also use BG to put the suspended process in the background) to continue running. In this way, you can flexibly switch between multiple tasks in one terminal.CodeIs particularly useful. Because when the code editor is suspended to the background and then placed back, the cursor position remains at the position of the last suspension, avoiding the trouble of locating again.
- Use
Disown-HJobspec
To enableA jobIgnore the HUP signal.
- Use
Disown-ah
To enableAll jobsThe HUP signal is ignored.
- Use
Disown-Rh
To enableRunning jobIgnore the HUP signal.
It should be noted that after disown is used, the target job will be removed from the job list and we will no longer be able to useJobs
To view it, but still can usePS-ef
Find it.
However, there is another problem. The operation object of this method is a job. If we add"&"To make it a job and run it in the background.Jobs
Command to obtain the list of all jobs. But if the current command is not run as a job, how can I get its job number? The answer is to press Ctrl-z (hold down the ctrl key while holding down the z key!
CTRL-Z is used to suspend the current process (suspend), and then we can useJobs
Command to query its job number, and then useBGJobspec
To put it in the background and continue running. Please use this method with caution if suspension affects the running result of the current process.
Disown Example 1 (if the command has been run in the background with "&", you can directly use "disown ")
[Root @ pvcent107 build] # cp-r testlargefile largefile & [1] 4825 [root @ pvcent107 build] # jobs [1] + running CP-I-r testlargefile largefile & [root @ pvcent107 build] # disown-H % 1 [root @ pvcent107 build] # ps-Ef | grep largefileroot 4825 968 1 00:00:00 pts/4 cp-I-r testlargefile largefileroot 4853 968 0 09:46 pts/4 00:00:00 grep largefile [root @ pvcent107 build] # logout
Disown Example 2 (if you do not use "&" to run the command in the background when submitting the command, you can use ctrl-Z and "BG" to put it in the background, and then use "disown ")
[Root @ pvcent107 build] # cp-r testlargefile largefile2 [1] + stopped CP-I-r testlargefile largefile2 [root @ pvcent107 build] # BG % 1 [1] + CP- i-r testlargefile largefile2 & [root @ pvcent107 build] # jobs [1] + running CP-I-r testlargefile largefile2 & [root @ pvcent107 build] # disown-H % 1 [root @ pvcent107 build] # ps-Ef | grep largefile2root 5790 5577 1 00:00:00 pts/3 00:00:00 CP-I-r testlargefile largefile2root 5824 5577 0 pts/3 grep largefile2 [root @ pvcent107 build] #
Screen
Scenario:
We already know how to protect the process from the HUP signal. But if a large number of such commands need to be run in a stable background, how can we avoid such operations on each command?
Solution:
screen is the most convenient method. Simply put, screen provides an ANSI/VT100 Terminal simulator to run multiple full-screen pseudo terminals under a real terminal. Screen has many parameters and has powerful functions. Here we will only introduce its common functions and briefly analyze why screen can avoid the impact of HUP signals. Let's take a look at the help information of screen:
Screen (1) screen (1) Name screen-screen manager with VT100/ANSI terminal emulationsynopsis screen [-options] [cmd [ARGs] screen-R [[pid.] tty [. host] screen-r sessionowner/[[pid.] tty [. host] Description screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells ). each virtual terminal provides the functions of a DEC VT100 Terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI x3.64) and ISO 2022 standards (e.g. insert/Delete line and support for multiple character sets ). there is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanic that allows moving text regions between windows.
Screen is easy to use and has the following common options:
- Use
Screen-DMsSession name
To create a session in disconnected mode (and specify its session name ).
- Use
Screen-list
To list all sessions.
- Use
Screen-RSession name
To reconnect to the specified session.
- Use shortcuts
CTRL-A D
To temporarily disconnect the current session.
Screen example
[Root @ pvcent107 ~] # Screen-DMs Urumchi [root @ pvcent107 ~] # Screen-listthere is a screen on: 12842. Urumchi (detached) 1 socket in/tmp/Screens/S-root. [root @ pvcent107 ~] # Screen-r Urumchi
When we use "-R" to connect to the screen session, we can do whatever we want in this Pseudo Terminal, and no longer have to worry about the impact of the HUP signal on our processes, you do not need to add "nohup" or "setsid" to each command. Why? Let me take a look at the two examples below.
1. Process tree of the new process when screen is not used
[Root @ pvcent107 ~] # Ping www.google.com & [1] 9499 [root @ pvcent107 ~] # Pstree-H 9499init-Restart-xvnc restart-acpid restart-ATD restart-2 * [Sendmail]├ ── Sshd-Example-Sshd ── bash ── pstree │Javas── sshd ── ─ bash ── Ping
We can see that when screen is not used, bash is a sub-process of sshd. When SSH is disconnected, the HUP signal will naturally affect all sub-processes (including the newly created Ping process) under it ).
2. Process tree of the new process after screen is used
[Root @ pvcent107 ~] # Screen-r Urumchi [root @ pvcent107 ~] # Ping www.ibm.com & [1] 9488 [root @ pvcent107 ~] # Pstree-H 9488init-Restart-xvnc restart-acpid restart-ATD├ ── Screen ── bash ── PingCertificate-2 * [Sendmail]
When screen is used, bash is the sub-process of screen, while screen is the sub-process of Init (PID is 1. When SSH is disconnected, the HUP signal will not affect the sub-processes under the screen.
Summary
Now several methods have been introduced. We can select different solutions based on different scenarios. Nohup/setsid is undoubtedly the most convenient method for temporary needs. disown can help us to remedy jobs that are already running afterwards, screen is the best choice for batch operations.