Nohup command Analysis
To put a command in the background for execution, we generally use nohup sh command &
& I know that this command is executed in the background. What does nohup do?
This starts from the unix signal. The unix signal mechanism can be said to be a type of inter-process communication. The process can send signals to complete some specific actions. It is familiar with kill-9 pid.
First, let's look at what signals linux has:
[Root @ limt ~] # Kill-l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN + 1 36) SIGRTMIN + 2 37) SIGRTMIN + 3
38) SIGRTMIN + 4 39) SIGRTMIN + 5 40) SIGRTMIN + 6 41) SIGRTMIN + 7 42) SIGRTMIN + 8
43) SIGRTMIN + 9 44) SIGRTMIN + 10 45) SIGRTMIN + 11 46) SIGRTMIN + 12 47) SIGRTMIN + 13
(48) SIGRTMIN + 14 49) SIGRTMIN + 15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
(53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8
(58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62)
63) SIGRTMAX-1 64) SIGRTMAX
[Root @ limt ~] # More/usr/include/bits/signum. h
# Define SIGHUP 1/* Hangup (POSIX ).*/
# Define SIGINT 2/* Interrupt (ANSI ).*/
# Define SIGQUIT 3/* Quit (POSIX ).*/
# Define SIGILL 4/* Illegal instruction (ANSI ).*/
# Define SIGTRAP 5/* Trace trap (POSIX ).*/
# Define SIGABRT 6/* Abort (ANSI ).*/
# Define SIGIOT 6/* IOT trap (4.2 BSD ).*/
# Define SIGBUS 7/* BUS error (4.2 BSD ).*/
# Define SIGFPE 8/* Floating-point exception (ANSI ).*/
# Define SIGKILL 9/* Kill, unblockable (POSIX ).*/
# Define SIGUSR1 10/* User-defined signal 1 (POSIX ).*/
# Define SIGSEGV 11/* Segmentation violation (ANSI ).*/
# Define SIGUSR2 12/* User-defined signal 2 (POSIX ).*/
# Define SIGPIPE 13/* Broken pipe (POSIX ).*/
# Define SIGALRM 14/* Alarm clock (POSIX ).*/
# Define SIGTERM 15/* Termination (ANSI ).*/
# Define SIGSTKFLT 16/* Stack fault .*/
# Define sigcld sigchld/* Same as SIGCHLD (System V ).*/
# Define SIGCHLD 17/* Child status has changed (POSIX ).*/
# Define SIGCONT 18/* Continue (POSIX ).*/
# Define SIGSTOP 19/* Stop, unblockable (POSIX ).*/
# Define SIGTSTP 20/* Keyboard stop (POSIX ).*/
# Define SIGTTIN 21/* Background read from tty (POSIX ).*/
# Define SIGTTOU 22/* Background write to tty (POSIX ).*/
# Define SIGURG 23/* Urgent condition on socket (4.2 BSD ).*/
# Define SIGXCPU 24/* CPU limit exceeded (4.2 BSD ).*/
# Define SIGXFSZ 25/* File size limit exceeded (4.2 BSD ).*/
# Define SIGVTALRM 26/* Virtual alarm clock (4.2 BSD ).*/
# Define SIGPROF 27/* Profiling alarm clock (4.2 BSD ).*/
# Define SIGWINCH 28/* Window size change (4.3 BSD, Sun ).*/
# Define sigpoll sigio/* Pollable event occurred (System V ).*/
# Define SIGIO 29/* I/O now possible (4.2 BSD ).*/
# Define SIGPWR 30/* Power failure restart (System V ).*/
# Define SIGSYS 31/* Bad system call .*/
Generally, each signal system has a default action (generally terminating the program). However, in addition to the SIGKILL and SIGSTOP signals, other signals can be captured and processed,
A process must signal to another process. You can use the kill-signal pid or call the kill function
Back to our previous question, why should we use nohup? When I exit with a terminal tool like Scrt, A SIGHUP signal will be sent to the process we started under the current shell, and the SIGHUP signal will terminate the process by default, so nohup means blocking the SIGHUP signal.
Next we will perform a test:
Run a background program without nohup in a window [root @ limt ~] # Sh Testlsof. sh> 111.log & [1] 4486 [root @ limt ~] # Jobs [1] + Running sh Testlsof. sh> 111.log & view the background program in another window [root @ limt ~] # Ps-ef | grep Testlsofroot 4486 4315 0 00:00:00 pts/1 sh Testlsof. shroot 4574 4500 0 00:00:00 pts/0 grep Testlsof close the first window, and the background process also exits [root @ limt ~] # Ps-ef | grep Testlsofroot 4661 4500 0 00:00:00 pts/0 grep Testlsof runs a background program with nohup in a window [root @ limt ~] # Nohup sh Testlsof. sh> 111.log & [1] 2710 [root @ limt ~] # Nohup: ignore input redirection errors to the standard output end [root @ limt ~] # Jobs [1] + Running nohup sh Testlsof. sh> 111.log & view the background program in another window [root @ limt ~] # Ps-ef | grep Testlsofroot 2710 2664 0 00:00:00 pts/1 sh Testlsof. sh // The parent process is shellroot 2794 2728 0 00:00:00 pts/2 grep Testlsof close the first window, and the background process does not exit [root @ limt ~] # Ps-ef | grep Testlsofroot 2710 1 0 20:23? 00:00:00 sh Testlsof. sh <span style = "font-family: Arial, Helvetica, sans-serif; "> // The parent process is the init process </span> root 3223 2728 0 00:00:00 pts/2 grep Testlsof
How does one prove that a SIGHUP signal is triggered when the terminal is closed? You can use the trap command to block SIGHUP. The value of SIGHUP is 1. Use the following command:
[Root @ limt ~] # Trap "" 1 then run a background program without nohup in a window [root @ limt ~] # Sh Testlsof. sh> 111.log & [1] 4068 [root @ limt ~] # Jobs [1] + Running sh Testlsof. sh> 111.log & view the background program in another window [root @ limt ~] # Ps-ef | grep Testroot 4068 2728 0 00:00:00 pts/2 sh Testlsof. shroot 4096 3963 0 00:00:00 pts/0 grep Test close the first window, the background process did not exit [root @ limt ~] # Ps-ef | grep Testroot 4068 1 0 20:26? 00:00:00 sh Testlsof. sh // when the first window is closed, its parent process is 1 root 4180 3963 0 00:00:00 pts/0 grep Test
If you are using csh, you do not need to use the nohup command, because csh handles SIGHUP.
[Root @ limt ~] # Csh [root @ limt ~] # Sh Testlsof. sh> 111.log & [1] 4602 [root @ limt ~] # Jobs [1] + Running sh Testlsof. sh> 111. log in another window to view the background program [root @ limt ~] # Ps-ef | grep Testlsofroot 4602 4509 0 00:00:00 pts/1 sh Testlsof. shroot 4658 4524 0 00:00:00 pts/0 grep Testlsof close the first window, the background process did not exit [root @ limt ~] # Ps-ef | grep Testlsofroot 4602 1 0 20:31? 00:00:00 sh Testlsof. shroot 4676 4524 0 00:00:00 pts/0 grep Testlsof