1
|
The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to ensure that they receive the SIGHUP.
|
1
2
|
|-sshd(2555)-+-sshd(16568)---
bash
(16572)---pstree(16862)
| `-sshd(16635)---
bash
(16637)---a.out(16860)---a.out(16861)
|
1
2
3
4
5
6
7
8
|
[root@xxx ~]
# cat test.sh
#!/bin/bash
sleep
300
[root@xxx ~]
#
[root@xxx ~]
# ./test.sh &
|
如果终端接口检测到调制解调器(或网络)已经断开连接,则将挂掉信号发送给控制进程(会话首进程)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[root@Betty ~]
# man bash
...
INVOCATION
A login shell is one whose first character of argument zero is a -, or one started with the --login option.
An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by
isatty(3)), or one started with the -i option. PS1 is
set
and $- includes i
if
bash
is interactive, allowing a shell script or a startup
file
to
test
this state.
...
SIGNALS
When
bash
is interactive,
in
the absence of any traps, it ignores SIGTERM (so that
kill
0 does not
kill
an interactive shell), and SIGINT is caught and handled (so that the wait
builtin
is interruptible). In all cases,
bash
ignores SIGQUIT. If job control is
in
effect,
bash
ignores SIGTTIN, SIGTTOU, and SIGTSTP.
Non-
builtin
commands run by
bash
have signal handlers
set
to the values inherited by the shell from its parent. When job control is not
in
effect, asynchronous commands ignore
SIGINT and SIGQUIT
in
addition to these inherited handlers. Commands run as a result of
command
substitution ignore the keyboard-generated job control signals SIGTTIN, SIGTTOU,
and SIGTSTP.
The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to
ensure that they receive the SIGHUP. To prevent the shell from sending the signal to a particular job, it should be removed from the jobs table with the disown
builtin
(see SHELL
BUILTIN COMMANDS below) or marked to not receive SIGHUP using disown -h.
If the huponexit shell option has been
set
with
shopt
,
bash
sends a SIGHUP to all jobs when an interactive login shell exits.
If
bash
is waiting
for
a
command
to complete and receives a signal
for
which
a
trap
has been
set
, the
trap
will not be executed
until
the
command
completes. When
bash
is waiting
for
an asynchronous
command
via the wait
builtin
, the reception of a signal
for
which
a
trap
has been
set
will cause the wait
builtin
to
return
immediately with an
exit
status
greater than 128, immediately after
which
the
trap
is executed.
...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include
#include
char
**args;
void
exithandle(
int
sig)
{
printf
(
"%s : sighup received\n"
,args[1]);
}
int
main(
int
argc,
char
**argv)
{
args=argv;
signal
(SIGHUP,exithandle);
pause();
return
0;
}
|
1
|
.
/sigtest
&
|
1
2
|
trap
""
SIGHUP
#该句的作用是屏蔽SIGHUP信号,trap可以屏蔽很多信号
sigtest
|
1
|
sigtest &
|
在这个环境下 GNU bash, version 3.2.25 ,前台进程,终端关闭后,进程会收到 2 次 SIGHUP 信号,一次为 bash 退出前由 bash 自己发送,一次为会话首进程(同样是 bash)退出后由 kernel 发送。