Linux in SIGHUP relationship with the nohup

SIGHUP signal and the control terminal

 
UNIX processes in organizational structure for the session (session) that contains a foreground process group and one or more of the background process group, a process group contains more than one process. A session may be a session first process, the first process and a session may have a control terminal. A process group may have a first process group process. The first process ID process group is equal to the process group ID. Here it is possible to have, under certain circumstances is not. Interacting with terminal foreground process is the process, otherwise it is a background process.
 
 SIGHUP will be sent to the corresponding process in the following three cases:
  1, when the terminal is closed, the signal is transmitted to the first session and the process as a process job submission (i.e., by the symbol & submission process)
  2, the first session when the process exits, the signal is sent to each process in the session foreground process group
  3, if the parent process exits the process leading to orphaned group consisting of, and the process group has processes in a stopped state (or SIGSTOP SIGTSTP received signal), the signal will be sent to each process in the process group.
 
Based system for SIGHUP default signal identified at processing is an end stop receives the signal into the process. Therefore, if the program does not capture the signal, upon receipt of the signal time , into the process exits.
 
Close observation of several terminal due to the following circumstances cause the process to exit, exit here because the process received a SIGHUP signal. login shell session is the first process.
 
First write a test program code is as follows:
#include <stdio.h>
#include  <signal.h>
char ** args;
void exithandle(int
 sig)
{
       printf("%s : sighup received ",args[1
]);
}

int main(int argc,char **argv)
{
       args =
 argv;
       signal(SIGHUP,exithandle);
       pause();
       return 0
;
}
 
After capturing program print a message SIGHUP signal, pause () program halt.
Compiled executable file as sigtest.
 
1, the command: sigtest front> tt.txt
   : Close terminal
   Results: The contents of the file is tt.txt front: sighup received
   The reason: sigtest a foreground process, the terminal is closed, according to Case 1 mentioned above, login shell session as the first process, will receive SIGHUP signal and then exit. According to the second case, sigtest as a foreground process, you will receive a SIGHUP signal sent login shell.
 
2, the command: sigtest back> tt.txt &
      : Close terminal
      Results: The contents of tt.txt file is back: sighup received
      The reason: sigtest job is submitted, according to Case 1 mentioned above, sigtest receive a SIGHUP signal.
 
3, the command: write a shell, content [sigtest &], and then execute the shell
      : Close terminal
      The results: ps -ef | grep sigtest see that the process is still, tt file is empty
      The reason: When execution of the shell, sigtest as job submission, then the shell exits, resulting in sigtest orphaned process, is no longer current session of the job, so that is not a session sigtest first process is not a job, do not receive SIGHUP . Meanwhile orphan process belongs to the background process, so after login shell exits SIGHUP is not sent to sigtest, because it sends the signal to the foreground process. Article 3 said that if the orphaned process group when the process group, if the process is stopped, will also receive a SIGHUP signal, but sigtest not at a standstill, it will not receive SIGHUP signal.
 
4, the command: nohup sigtest> tt
      : Close terminal
      The results: tt file is empty
      The reason: nohup prevents the process receives a SIGHUP signal
 
At this point, we know the circumstances under which the process will exit after the terminal is turned off, will not quit under any circumstances.

 

To close the terminal after the process does not quit there are several ways, all the way through the shell:
 1, the preparation of shell, as follows
       trap "" SIGHUP # This sentence is the role of shielding SIGHUP signal, trap can mask a lot of signals
       sigtest
 2, nohup sigtest can be executed directly from the command line,
       To finish the operation continues after another operation, nohup sigtest &
 3, the preparation of shell, as follows
       sigtest &
       In fact, any way the process can become an orphan process, including the fork immediately after the parent process exits.
 
Signal (SIGHUP, SIG_IGN);
Signal function signal, a signal indicating the value of the first parameter (SIGHUP) to be processed, the second parameter is represented by a function or process, where, SIG_IGN SIGHUP omit registration of that signal.
SIGHUP
and console related operations, when the console is turned off when the system will send a HUP signal to all processes have sessionID console, the default action is HUP signal of
exit, if remote login to start a service process and close the connection when the program runs it will lead to service process exits, so the process will start with a general service nohup tool or written as a daemon.
 

, Sighup with nohup
SIGHUP (hang up) signals:
a control terminal or control issue related to the session in progress when the death process, the default process SIGHUP signal processing is to terminate the program, so the program we created in the shell, in log out after the connection is lost, it will also sign.

Online search to explain: When will send SIGHUP signal? When a group consisting of orphaned process, in the process of claim POSIX.1 stopped state to set the transmission orphaned SIGHUP (hang up) signal, the system default handling of such signals for termination of the process, however, if the signal or to ignore this dealt with separately, then this process can continue to suspend execution.

nohup command:
named Incredibles is to ignore SIGHUP signal, usually used in conjunction with &, & for this program to submit to a background job or background process group. Execute the following command

nohup bash -c "tail -f /var/log/messages | grep sys" &
1

nohup and & program starts, when the terminal is not yet closed, completely unlike the traditional daemon, because it is not the first process and a session held terminal, but it ignores the SIGHUP signal

Nohup can see from the source, in fact nohup only had three things
dofile function redirect the output to a file nohup.out
signal SIGHUP signal handler function is set to SIG_IGN macro (pointing sigignore function), this signal is ignored SIG_HUP
execvp function replace the current process with a new program code segment, data segment and stack segment and stack segment.
After execvp function is executed, a new program (does not fork process) will inherit some of the calling process attributes, such as: process id, session id, control terminals

After disconnected Login

After the terminal is turned off, nohup play a similar effect daemon, but with the traditional daemon is still a difference

nohup process created the working directory is the directory where you execute the command
012 standard input output standard error standard file pointing to nohup.out
process nohup created the group, in addition to the head of the parent process id of the process becomes 1, and the remaining process still retain the original session id, process group id, the parent process id, remain the same
online search to explain: nohup command to suspend the program in a way to ignore the signal up and running, output is running the program will not display to the terminal.

Whether or not the nohup command output redirected to the terminal, the output will be appended to the current directory nohup.out file. If nohup.out current directory
is not writable, output is redirected to $ HOME / nohup.out file. If the file can not be created or opened for append, then the command
parameter specifies the command is not invoked. If the standard error is a terminal, then the output of all the written standard error of the specified command is redirected to the same standard output file descriptor.

 

linux——signal信号(SIGHUP、SIGINT、SIGQUIT、SIGILL、SIGTRAP、SIGABRT...........................)

https://blog.csdn.net/u014470361/article/details/83591513

Guess you like

Origin www.cnblogs.com/klb561/p/12051068.html