信号

信号的基本属性可以总结为两方面,即:软中断和时延性。

例如:A给B发送信号,B收到信号之前执行自己的代码,收到信号后,不管执行到程序的什么位置,都要暂停运行,去处理信号,处理完毕再继续执行。与硬件中断类似——异步模式。

但信号是软件层面上实现的中断,早期常被称为“软中断”。由于信号是通过软件方法实现,其实现手段导致信号有很强的延时性。但对于用户来说,这个延迟时间非常短,不易察觉。

查看各类型的信号的具体含义可以用过man 7 signal 命令来查看

 Standard signals
       Linux supports the standard signals listed below.  Several signal numbers are architecture-dependent, as indicated in the "Value" column.  (Where three values are given, the first one is usu‐
       ally valid for alpha and sparc, the middle one for x86, arm, and most other architectures, and the last one for mips.  (Values for parisc are not shown; see the Linux kernel source for signal
       numbering on that architecture.)  A - denotes that a signal is absent on the corresponding architecture.)
 
       First the signals described in the original POSIX.1-1990 standard.
 
       Signal     Value     Action   Comment
       ──────────────────────────────────────────────────────────────────────
       SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process
       SIGINT        2       Term    Interrupt from keyboard
       SIGQUIT       3       Core    Quit from keyboard
       SIGILL        4       Core    Illegal Instruction
       SIGABRT       6       Core    Abort signal from abort(3)
       SIGFPE        8       Core    Floating point exception
       SIGKILL       9       Term    Kill signal
       SIGSEGV      11       Core    Invalid memory reference
       SIGPIPE      13       Term    Broken pipe: write to pipe with no
                                     readers
       SIGALRM      14       Term    Timer signal from alarm(2)
       SIGTERM      15       Term    Termination signal
       SIGUSR1   30,10,16    Term    User-defined signal 1
       SIGUSR2   31,12,17    Term    User-defined signal 2
       SIGCHLD   20,17,18    Ign     Child stopped or terminated
       SIGCONT   19,18,25    Cont    Continue if stopped
       SIGSTOP   17,19,23    Stop    Stop process
       SIGTSTP   18,20,24    Stop    Stop typed at terminal
       SIGTTIN   21,21,26    Stop    Terminal input for background process
       SIGTTOU   22,22,27    Stop    Terminal output for background process
 
       The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
 
       Next the signals not in the POSIX.1-1990 standard but described in SUSv2 and POSIX.1-2001.
 
       Signal       Value     Action   Comment
       ────────────────────────────────────────────────────────────────────
       SIGBUS      10,7,10     Core    Bus error (bad memory access)
       SIGPOLL                 Term    Pollable event (Sys V).
                                       Synonym for SIGIO
       SIGPROF     27,27,29    Term    Profiling timer expired
       SIGSYS      12,31,12    Core    Bad argument to routine (SVr4)
       SIGTRAP        5        Core    Trace/breakpoint trap
       SIGURG      16,23,21    Ign     Urgent condition on socket (4.2BSD)
       SIGVTALRM   26,26,28    Term    Virtual alarm clock (4.2BSD)
       SIGXCPU     24,24,30    Core    CPU time limit exceeded (4.2BSD)
 
       SIGXFSZ     25,25,31    Core    File size limit exceeded (4.2BSD)
 
       Up  to  and including Linux 2.2, the default behavior for SIGSYS, SIGXCPU, SIGXFSZ, and (on architectures other than SPARC and MIPS) SIGBUS was to terminate the process (without a core dump).
       (On some other UNIX systems the default action for SIGXCPU and SIGXFSZ is to terminate the process without a core dump.)  Linux 2.4 conforms to the POSIX.1-2001 requirements  for  these  sig‐
       nals, terminating the process with a core dump.
 
       Next various other signals.
 
       Signal       Value     Action   Comment
       ────────────────────────────────────────────────────────────────────
       SIGIOT         6        Core    IOT trap. A synonym for SIGABRT
       SIGEMT       7,-,7      Term
       SIGSTKFLT    -,16,-     Term    Stack fault on coprocessor (unused)
       SIGIO       23,29,22    Term    I/O now possible (4.2BSD)
       SIGCLD       -,-,18     Ign     A synonym for SIGCHLD
       SIGPWR      29,30,19    Term    Power failure (System V)
       SIGINFO      29,-,-             A synonym for SIGPWR
       SIGLOST      -,-,-      Term    File lock lost (unused)
       SIGWINCH    28,28,20    Ign     Window resize signal (4.3BSD, Sun)
       SIGUNUSED    -,31,-     Core    Synonymous with SIGSYS
 
       (Signal 29 is SIGINFO / SIGPWR on an alpha but SIGLOST on a sparc.)
 
       SIGEMT is not specified in POSIX.1-2001, but nevertheless appears on most other UNIX systems, where its default action is typically to terminate the process with a core dump.
 
       SIGPWR (which is not specified in POSIX.1-2001) is typically ignored by default on those other UNIX systems where it appears.
 
       SIGIO (which is not specified in POSIX.1-2001) is ignored by default on several other UNIX systems.
 
       Where defined, SIGUNUSED is synonymous with SIGSYS on most architectures.

通过执行kill -l 列出当前系统支持的信号类型及其编号:

[root@cyl cyl]#  kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 2) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
1)  SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
2)  SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
3)  SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
4)  SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
5)  SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
6)  SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
7)  SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
8)  SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
9)  SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
10) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
11) SIGRTMAX-1	64) SIGRTMAX
   

信号简要说明:

SIGHUP            终止进程    终端线路挂断
SIGINT         终止进程    中断进程
SIGQUIT       建立CORE文件   终止进程,并且生成core文件
SIGILL          建立CORE文件      非法指令
SIGTRAP       建立CORE文件      跟踪自陷
SIGBUS        建立CORE文件      总线错误
SIGSEGV      建立CORE文件      段非法错误
SIGFPE         建立CORE文件      浮点异常
SIGIOT         建立CORE文件      执行I/O自陷
SIGKILL        终止进程    杀死进程
SIGPIPE        终止进程    向一个没有读进程的管道写数据
SIGALARM     终止进程    计时器到时
SIGTERM       终止进程    软件终止信号
SIGSTOP       停止进程    非终端来的停止信号
SIGTSTP       停止进程    终端来的停止信号
SIGCONT      忽略信号    继续执行一个停止的进程
SIGURG        忽略信号    I/O紧急信号
SIGIO           忽略信号    描述符上可以进行I/O
SIGCHLD      忽略信号    当子进程停止或退出时通知父进程
SIGTTOU      停止进程    后台进程写终端
SIGTTIN       停止进程    后台进程读终端
SIGXGPU      终止进程    CPU时限超时
SIGXFSZ      终止进程    文件长度过长
SIGWINCH   忽略信号    窗口大小发生变化
SIGPROF      终止进程    统计分布图用计时器到时
SIGUSR1      终止进程    用户定义信号1
SIGUSR2      终止进程    用户定义信号2
SIGVTALRM  终止进程    虚拟计时器到时

有两个信号可以停止进程:SIGTERM和SIGKILL。 SIGTERM比较友好,进程能捕捉这个信号,根据您的需要来关闭程序。在关闭程序之前,您可以结束打开的记录文件和完成正在做的任务。在某些情况下,假 如进程正在进行作业而且不能中断,那么进程可以忽略这个SIGTERM信号。

对于SIGKILL信号,进程是不能忽略的。这是一个 “我不管您在做什么,立刻停止”的信号。假如您发送SIGKILL信号给进程,Linux就将进程停止在那里。

nohup命令

nohup 英文全称 no hang up(不挂起),用于在系统后台不挂断地运行命令,退出终端不会影响程序的运行。

nohup 命令,在默认情况下(非重定向时),会输出一个名叫 nohup.out 的文件到当前目录下,如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。

nohup的本质是通过忽略SIGHUP信号,从而不会在终端关闭会退出时由于SIGHUP信号导致程序退出,nohup本身不会将程序放入后台运行,但是通过nohup方式启动的程序,由于其忽略了SIGHUP信号,其接收到SIGHUP信号也不会退出。

语法格式

nohup Command [ Arg … ] [&]

参数说明
Command:要执行的命令。

Arg:一些参数,可以指定输出文件。

&:让命令在后台执行,终端退出后命令仍旧执行。

大多数人都会见到如下方式使用, 执行命令并重定向输入到xx.log文件中

nohup Command > xx.log 2>&1 &

2>&1 解释:

将标准错误 2 重定向到标准输出 &1 ,标准输出 &1 再被重定向输入到文件中。

0 – stdin (standard input,标准输入)
1 – stdout (standard output,标准输出)
2 – stderr (standard error,标准错误输出)

&命令

就是将程序放在后台运行。对于shell来说,通过& 可以把一个程序放在后台运行。其就是作为一个job来运行的。此时如果终端断开,程序还是会被SIGHUP信号导致退出的

语法格式

command   [Arg ...]   &

参数说明

Command:要执行的命令。

Arg:一些参数,可以指定输出文件。

总结

  1. nohup执行后默认会将输出保存在nohup.out文件,但执行后无法接受标准输入,关闭窗口后仍然会运行
  2. &无法将标准输出保存到文件中,可以接受标准输入,关闭窗口后程序就会停止
  3. nohup &一起使用综合了两者的优点,既能标准输入也能将标准输出的日志输入到文件,关闭窗口后仍然会运行

参考链接

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐