在/etc/init.d/logd 脚本中,
start ()
{
logd -d &
}
发现在后台执行的方式启动守护进程,其SIGINT和SIGQUIT被设置成为SIG_IGN,导致无法通过SIGINT杀死。
start ()
{
logd -d
}
这种方式就没问题。原因:
根据 The Shell and Utilities volume of POSIX.1-2017
If job control is disabled (see the description of set -m) when the shell executes an asynchronous list, the commands in the list shall inherit from the shell a signal action of ignored (SIG_IGN) for the SIGINT and SIGQUIT signals. In all other cases, commands executed by the shell shall inherit the same signal actions as those inherited by the shell from its parent unless a signal action is modified by the trap special built-in (see trap)
如果shell的job control是关闭的情况下,那么在后台运行的命令,将忽略SIGINT和SIGQUIT。这样防止用户按CTRL+C时,把后台进程杀死。
通常只有交互式shell的job control是打开的,rc.d是脚本方式运行的,显然不是交互式shell。
根据bash手册:下列场景下才是交互式shell
1 shell执行时,没有非选项参数,除非指定了-s。
2 没有指定-c选项。
3 input和error output都连接到terminals。(isatty)
4 或者以-i选项启动。
-s选项
以交互式shell方式运行,且把非选项参数当成positional parameters,而不是脚本路径。
yuan@yuan-vm:/work/test $ sh -s 123
$ echo $@
123
https://stackoverflow.com/questions/60626955/why-bash-background-task-ignores-sigint
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_11