When we deploy and maintain multiple hosts, it is often necessary to execute some commands remotely through a program, but it is common to find commands that are normally executed locally that are not normal at remote calls. This is usually because remote calls run as: Telnet-"Call-" to log out, and at ' exit ' the terminal receives a HUP (hangup) signal to close all its child processes (which naturally contain our ' call ' commands). 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
As the name implies, Nohup's purpose is to let the submitted command ignore the hangup signal.
generally we can add at the end "&" to run the command in the ">filename 2>&1"
background as well as to change the default redirection file name.
nohup Example
[[email protected] ~]# nohup ping www.baidu.com &[1] 3059nohup:appending output to ' nohup.out ' [[email protected] ~]# Ps-ef |grep 3059root 3059 984 0 21:06 pts/3 00:00:00 ping www.baidu.comroot 3067 984 0 21:06 pts/3 00:00:00 grep 3059[[email protected] ~]#
2. Setsid
Setsid can make the process not belong to the sub-process of the terminal receiving the HUP signal, then naturally it will not be affected by the HUP signal.
Setsid usage:
Setsid--help
Usage:
Setsid [Options] <program> [arguments ...]
Options:
-C,--ctty set the controlling terminal to the current one
-H,--help display this help and exit
-V,--version output version information and exit
For more details see Setsid (1).
Visible Setsid is also very convenient to use, but also only need to deal with the command before adding Setsid can.
Setsid Example
[[email protected] ~]# setsid ping www.baidu.com[[email protected] ~]# ps-ef |grep www.baidu.comroot 31094 1 0 07:28? 00:00:00 Ping www.baidu.comroot 31102 29217 0 07:29 pts/4 00:00:00 grep www.baidu.com
It is worth noting that our process ID (PID) in the example above is 31094, and its parent ID (PPID) is 1 (that is, the Init process ID), not the process ID of the current terminal.
3. &
The inclusion of one or more names in "()" allows these commands to run in a child shell, and when we put "&" into "()", we will find that the submitted job is not in the job list, that is, it cannot be jobs
viewed. So it will be able to avoid the impact of the HUP signal.
Subshell Example
[[Email protected] ~]# (ping www.baidu.com &) [[email protected] ~]# ps-ef |grep www.baidu.comroot 16270
1 0 14:13 pts/4 00:00:00 ping www.baidu.comroot 16278 15362 0 14:13 pts/4 00:00:00 grep www.baidu.com
As can be seen from the example above, 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.
The problem of unsuccessful remote execution commands