linux signal 笔记

sigterm可以捕捉、可以忽略,通常作为优雅关闭的方式。sigkill不可以捕捉、不可以忽略,是杀死进程的最后方式。

几个容易混淆的信号,以及trap命令

sigkill

The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal.

信号不可以被捕捉、不可以被忽略(init进程除外)。 应该把sigkill作为杀死进程的最后方式。(kill -9

sigterm

The SIGTERM signal is sent to a process to request its termination. Unlike the SIGKILL signal, it can be caught and interpreted or ignored by the process. This allows the process to perform nice termination releasing resources and saving state if appropriate. SIGINT is nearly identical to SIGTERM.

信号可以被进程捕捉、忽略。 sigterm可以实现进程的优雅关闭。(kill -15) 类似sigint,但是不限于终端程序。

sigint

The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Ctrl+C, but on some systems, the “delete” character or “break” key can be used

在终端里需要中断程序,使用ctrl + c 触发。

sigquit

The SIGQUIT signal is sent to a process by its controlling terminal when the user requests that the process quit and perform a core dump.

在终端触发,会core dump。

sighup

The SIGHUP signal is sent to a process when its controlling terminal is closed. It was originally designed to notify the process of a serial line drop (a hangup). In modern systems, this signal usually means that the controlling pseudo or virtual terminal has been closed.[4] Many daemons will reload their configuration files and reopen their logfiles instead of exiting when receiving this signal.[5] nohup is a command to make a command ignore the signal.

用于通知进程,终端已经关闭。 通常是在终端的控制进程结束时, 通知同一session内的各个作业, 这时它们与控制终端不再关联。 nohup命令可以让进程忽略sighup信号。

信号0

在keepalived健康检查脚本,看到同事使用killall -0 keepalived来检测进程。 第一次见到信号0:

“signal 0” is kind of like a moral equivalent of “ping”. Using “kill -0 NNN” in a shell script is a good way to tell if PID “NNN” is alive or not: signal 0 is just used to check process is exists or not.

trap命令

trap命令可以在shell脚本中捕捉signal。

test.sh如下:

#! /bin/bash

trap "echo TRAP SIGTERM" TERM

for i in `seq 1 100`
do
    echo $i
    sleep 1
done

运行脚本

# 窗口1
chmod u+x test.sh
./test.sh

# 窗口2
ps ax | grep test.sh | head -1 | awk {'print $1'} | xargs kill -15

参考

Built with Hugo
Theme Stack designed by Jimmy