gpt4 book ai didi

linux - 如何在 Linux 中向应用程序发出信号而不杀死它?

转载 作者:IT老高 更新时间:2023-10-28 12:40:18 26 4
gpt4 key购买 nike

我有一个看门狗应用程序。它监视我的主应用程序,该应用程序可能由于某种原因崩溃(我知道这很糟糕,但这不是重点)。

我将此看门狗编程为接受 SIGUSR1 信号以停止监视我的应用程序的存在。我用

发出信号
kill -SIGUSR1 `pidof myapp`

这真的很好用。当我尝试向没有内置此功能的旧版本看门狗发出信号时,我的问题就出现了。在这种情况下,kill 信号会杀死看门狗(终止进程),这会导致进一步的并发症(重新启动设备) .

有没有办法用 SIGUSR1 向我的看门狗发出信号,以便在这个特定信号未处理时它不会终止?

最佳答案

来自 GNU docs关于信号处理:

The SIGUSR1 and SIGUSR2 signals are set aside for you to use any way you want. They're useful for simple interprocess communication, if you write a signal handler for them in the program that receives the signal.There is an example showing the use of SIGUSR1 and SIGUSR2 in section Signaling Another Process.The default action is to terminate the process.

SIGINFO 的默认 Action 是什么都不做,所以可能更合适:

SIGINFO: Information request. In 4.4 BSD and the GNU system, this signal is sent to all the processes in the foreground process group of the controlling terminal when the user types the STATUS character in canonical mode; see section Characters that Cause Signals.If the process is the leader of the process group, the default action is to print some status information about the system and what the process is doing. Otherwise the default is to do nothing.

SIGHUP当控制终端关闭时发出,但由于大多数守护程序未连接到终端,因此将其用作“重新加载”并不少见:

Daemon programs sometimes use SIGHUP as a signal to restart themselves, the most common reason for this being to re-read a configuration file that has been changed.

顺便说一句,您的看门狗可能会不时读取配置文件,以了解它是否应该重新启动该进程。

我个人最喜欢看门狗是supervisor .

$ supervisorctl start someapp
someapp: started

$ supervisorctl status someapp
someapp RUNNING pid 16583, uptime 19:16:26

$ supervisorctl stop someapp
someapp: stopped

查看 kill -l 是否返回您平台上的信号列表并尝试其中一些,但 SIGUSR1 似乎是一个糟糕的选择。

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

[更新]

Carpetsmoker 评论 Linux 和 BSD 之间的行为差​​异:

SIGINFO seems to work different on GNU libc & BSD; on BSD, it works as you describe, but on Linux, it either doesn't exist, or is the same as SIGPWR... The GNU libc manual seems incorrect in this regard (your kill -l output also doesn't show SIGINFO)... I don't know why GNU doesn't support it, because I find it to be very useful... – Carpetsmoker

关于linux - 如何在 Linux 中向应用程序发出信号而不杀死它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10824886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com