为什么你的kill命令不能列出信号表

有时候你想用 kill 命令给某些程序发送特殊信号,一时之间你不知道信号名、编号或者怕拼写错误,,此刻 kill 命令自带的 kill -l 或者 kill -L 命令或许能帮到你。

部分读者可能遇到这种状况:

1➜  ~ kill -l
2HUP INT QUIT ILL TRAP IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS
3➜  ~ kill -L
4kill: unknown signal: SIGL
5kill: type kill -l for a list of signals

看上面的输出:

  • kill -l 也只是显示个简单列表,没有编号;

  • kill -L 居然会报错;

这一切很有可能是你用了 zsh

1➜  ~ echo $SHELL
2/usr/bin/zsh

如果你切换到 bash,熟悉的界面就回来了。

 1mephisto@pc:~$ kill -l
 2 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 3 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
 411) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
 516) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
 621) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
 726) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
 831) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
 938) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
1043) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
1148) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
1253) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
1358) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
1463) SIGRTMAX-1  64) SIGRTMAX
15mephisto@pc:~$ kill -L
16 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
17 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
1811) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
1916) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
2021) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
2126) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
2231) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
2338) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
2443) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
2548) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
2653) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
2758) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
2863) SIGRTMAX-1  64) SIGRTMAX

为什么会这样呢? 请看下面的输出(zsh 环境)。

1➜  ~ type -a kill
2kill is a shell builtin
3kill is /usr/bin/kill
4kill is /bin/kill
5➜  ~ md5sum /usr/bin/kill /bin/kill
6bec5c7ac875a475f103603d8170dbc31  /usr/bin/kill
7bec5c7ac875a475f103603d8170dbc31  /bin/kill
8➜  ~ echo $SHELL
9/usr/bin/zsh

目前 bash 和 zsh 都有 builtin 的 kill 命令(优先级高,率先命中),相关文档说明:

当你在 bashzsh 中使用 kill 命令时,优先命中的都是 shell 自带的 kill 命令,二者的输出差异导致上述不同。

独立的 kill 命令 可使用 /bin/kill 调用(在笔者机器上,/usr/bin/kill 是相同程序,md5 码相同):

1➜  ~ /bin/kill -l
2HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT
3CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS
4➜  ~ /bin/kill -L
5 1 HUP      2 INT      3 QUIT     4 ILL      5 TRAP     6 ABRT     7 BUS
6 8 FPE      9 KILL    10 USR1    11 SEGV    12 USR2    13 PIPE    14 ALRM
715 TERM    16 STKFLT  17 CHLD    18 CONT    19 STOP    20 TSTP    21 TTIN
822 TTOU    23 URG     24 XCPU    25 XFSZ    26 VTALRM  27 PROF    28 WINCH
929 POLL    30 PWR     31 SYS

输出正常,比 bash 内置的编号少(少了 31 以上的)。

原始信号定义在这两个头文件中 /usr/include/asm/signal.h/usr/include/asm/siginfo.h

 1  ~ cat /usr/include/linux/signal.h
 2/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
 3#ifndef _LINUX_SIGNAL_H
 4#define _LINUX_SIGNAL_H
 5
 6#include <asm/signal.h>
 7#include <asm/siginfo.h>
 8
 9#define SS_ONSTACK      1
10#define SS_DISABLE      2
11
12/* bit-flags */
13#define SS_AUTODISARM   (1U << 31)      /* disable sas during sighandling */
14/* mask for all SS_xxx flags */
15#define SS_FLAG_BITS    SS_AUTODISARM
16
17#endif /* _LINUX_SIGNAL_H */

看看头文件:

 1 ~ head -n 62 /usr/include/asm/signal.h
 2/_ SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note _/
 3#ifndef \_ASM_X86_SIGNAL_H
 4#define \_ASM_X86_SIGNAL_H
 5
 6#ifndef **ASSEMBLY**
 7#include <linux/types.h>
 8#include <linux/time.h>
 9
10/_ Avoid too many header ordering problems. _/
11struct siginfo;
12
13/_ Here we must cater to libcs that poke about in kernel headers. _/
14
15#define NSIG 32
16typedef unsigned long sigset_t;
17
18#endif /_ **ASSEMBLY** _/
19
20#define SIGHUP 1
21#define SIGINT 2
22#define SIGQUIT 3
23#define SIGILL 4
24#define SIGTRAP 5
25#define SIGABRT 6
26#define SIGIOT 6
27#define SIGBUS 7
28#define SIGFPE 8
29#define SIGKILL 9
30#define SIGUSR1 10
31#define SIGSEGV 11
32#define SIGUSR2 12
33#define SIGPIPE 13
34#define SIGALRM 14
35#define SIGTERM 15
36#define SIGSTKFLT 16
37#define SIGCHLD 17
38#define SIGCONT 18
39#define SIGSTOP 19
40#define SIGTSTP 20
41#define SIGTTIN 21
42#define SIGTTOU 22
43#define SIGURG 23
44#define SIGXCPU 24
45#define SIGXFSZ 25
46#define SIGVTALRM 26
47#define SIGPROF 27
48#define SIGWINCH 28
49#define SIGIO 29
50#define SIGPOLL SIGIO
51/_
52#define SIGLOST 29
53_/
54#define SIGPWR 30
55#define SIGSYS 31
56#define SIGUNUSED 31
57
58/_ These should not be considered constants from userland. _/
59#define SIGRTMIN 32
60#define SIGRTMAX \_NSIG

各种信号的详细解释请看帮助文档,里面解释的比较到位,包括实时信号的相关介绍。

常用信号:

  • HUP 1 终端挂断
  • INT 2 中断(同 Ctrl + C)
  • QUIT 3 退出(同 Ctrl + \)
  • KILL 9 强制终止
  • TERM 15 终止(Default action is to terminate the process)
  • CONT 18 继续(与 STOP 相反,fg/bg 命令)
  • STOP 19 暂停(同 Ctrl + Z)

KILL 编号 9,好比游戏里面的无可奈何、放终极大招了。

CONT/STOP 读者可以简单测试下,控制 yes、tail 等命令的输出,还挺有趣的。

TERM 信号是默认的,通常程序收到该信号后,会触发相关代码,做一些收尾动作,然后退出,即所谓的优雅退出。

比如 k8s:

Typically, with this graceful termination of the pod, kubelet makes requests to the container runtime to attempt to stop the containers in the pod by first sending a TERM (aka. SIGTERM) signal, with a grace period timeout, to the main process in each container. The requests to stop the containers are processed by the container runtime asynchronously. There is no guarantee to the order of processing for these requests. Many container runtimes respect the STOPSIGNAL value defined in the container image and, if different, send the container image configured STOPSIGNAL instead of TERM. Once the grace period has expired, the KILL signal is sent to any remaining processes, and the Pod is then deleted from the API Server. If the kubelet or the container runtime's management service is restarted while waiting for processes to terminate, the cluster retries from the start including the full original grace period.

各个软件处理信号并不一致,Nginx 就有所不同,它的优雅关闭是发送 SIGQUIT 😅:

1nginx -s <SIGNAL>
2
3where <SIGNAL> can be one of the following:
4
5    quit – Shut down gracefully (the SIGQUIT signal)
6    reload – Reload the configuration file (the SIGHUP signal)
7    reopen – Reopen log files (the SIGUSR1 signal)
8    stop – Shut down immediately (or fast shutdown, the SIGTERM singal)

再比如 gunicorn,符合套路,常规出牌的:

 1
 2Master process
 3
 4    QUIT, INT: Quick shutdown
 5    TERM: Graceful shutdown. Waits for workers to finish their current requests up to the graceful_timeout.
 6    HUP: Reload the configuration, start the new worker processes with a new configuration and gracefully shutdown older workers. If the application is not preloaded (using the preload_app option), Gunicorn will also load the new version of it.
 7    TTIN: Increment the number of processes by one
 8    TTOU: Decrement the number of processes by one
 9    USR1: Reopen the log files
10    USR2: Upgrade Gunicorn on the fly. A separate TERM signal should be used to kill the old master process. This signal can also be used to use the new versions of pre-loaded applications. See Upgrading to a new binary on the fly for more information.
11    WINCH: Gracefully shutdown the worker processes when Gunicorn is daemonized.

这让人想起显示软件版本有的 -v-V--verson-version,每次都很感慨,只能说世界是丰富多彩的。

伟人安慰语:

“吾生也有涯,而知也無涯” -- 莊子

最后修改于: Wednesday, November 15, 2023
欢迎关注微信公众号,留言交流。

相关文章:

翻译: