TOC

Linux 信号

信号

信号可以被捕获,或者被忽略,除了 9 KILL 和 19 STOP

信号值

$ kill -l
HUP 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

$ type kill
kill is a shell builtin

$ /usr/bin/kill -L
 1 HUP      2 INT      3 QUIT     4 ILL      5 TRAP     6 ABRT     7 BUS
 8 FPE      9 KILL    10 USR1    11 SEGV    12 USR2    13 PIPE    14 ALRM
15 TERM    16 STKFLT  17 CHLD    18 CONT    19 STOP    20 TSTP    21 TTIN
22 TTOU    23 URG     24 XCPU    25 XFSZ    26 VTALRM  27 PROF    28 WINCH
29 POLL    30 PWR     31 SYS
  1. HUP 终端关闭
  2. INT Ctrl + C
  3. QUIT 终端退出 Ctrl + 4 or \
  4. ILL
  5. TRAP
  6. IOT/ABRT
  7. BUS
  8. FPE
  9. KILL 常来自 kill -9
  10. USR1
  11. SEGV
  12. USR2
  13. PIPE
  14. ALRM
  15. TERM
  16. STKFLT
  17. CHLD 子进程终止
  18. CONT
  19. STOP
  20. TSTP 终端挂起 Ctrl + Z
  21. TTIN
  22. TTOU
  23. URG
  24. XCPU
  25. XFSZ
  26. VTALRM
  27. PROF
  28. WINCH 终端窗口大小改变
  29. POLL
  30. PWR
  31. SYS

由于 9 KILL 不能被捕获,设计软件时可以捕获 15 TERM, 进行清理操作,然后需要杀进程时,先尝试 kill -15

附: 信号表(来自 man 7 signal

Signal x86/ARM
most others
Alpha/
SPARC
MIPS PARISC Notes
SIGHUP 1 1 1 1
SIGINT 2 2 2 2
SIGQUIT 3 3 3 3
SIGILL 4 4 4 4
SIGTRAP 5 5 5 5
SIGABRT 6 6 6 6
SIGIOT 6 6 6 6
SIGBUS 7 10 10 10
SIGEMT - 7 7 -
SIGFPE 8 8 8 8
SIGKILL 9 9 9 9
SIGUSR1 10 30 16 16
SIGSEGV 11 11 11 11
SIGUSR2 12 31 17 17
SIGPIPE 13 13 13 13
SIGALRM 14 14 14 14
SIGTERM 15 15 15 15
SIGSTKFLT 16 - - 7
SIGCHLD 17 20 18 18
SIGCLD - - 18 -
SIGCONT 18 19 25 26
SIGSTOP 19 17 23 24
SIGTSTP 20 18 24 25
SIGTTIN 21 21 26 27
SIGTTOU 22 22 27 28
SIGURG 23 16 21 29
SIGXCPU 24 24 30 12
SIGXFSZ 25 25 31 30
SIGVTALRM 26 26 28 20
SIGPROF 27 27 29 21
SIGWINCH 28 28 20 23
SIGIO 29 23 22 22
SIGPOLL Same as SIGIO
SIGPWR 30 29/- 19 19
SIGINFO - 29/- - -
SIGLOST - -/29 - -
SIGSYS 31 12 12 31
SIGUNUSED 31 - - 31