如何在不杀死Linux的情况下发信号通知应用程序? [英] How to signal an application without killing it in Linux?

查看:195
本文介绍了如何在不杀死Linux的情况下发信号通知应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a watchdog application. It watches my main app which might crash for one reason or another (I know it is bad, but this is not the point).

我将此看门狗编程为接受SIGUSR1信号以停止监视我的应用程序状态.我用

I programmed this watchdog to accept SIGUSR1 signals to stop monitoring my application presence. I signal it with

kill -SIGUSR1 `pidof myapp`

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

This works really well. My problem comes when I try to signal an older version of the watchdog which does not have this functionality built in. In this case, the kill signal kills the watchdog (terminates the process), which leads to further complications (rebooting of the device).

是否可以通过SIGUSR1向看门狗发出信号,以便在未处理此特定信号时不会终止它?

Is there a way to signal my watchdog with SIGUSR1 so that it does not terminate if this particular signal is unhandled?

推荐答案

来自

已将SIGUSR1和SIGUSR2信号留出,以便您使用所需的任何方式.如果在接收信号的程序中为它们编写信号处理程序,则它们对于简单的进程间通信很有用. 在发信号通知另一个进程"部分中有一个示例显示了SIGUSR1和SIGUSR2的用法. 默认操作是终止该过程.

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的默认操作是不执行任何操作,因此它可能更适合:

The default action for SIGINFO is to do nothing, so it may be more suitable:

SIGINFO:信息请求.在4.4 BSD和GNU系统中,当用户以标准模式键入STATUS字符时,此信号将发送到控制终端的前台进程组中的所有进程.请参阅导致信号的字符一节. 如果流程是流程组的负责人,则默认操作是打印一些有关系统以及流程正在执行的状态信息. 否则默认为不执行任何操作.

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 ,但是由于大多数守护程序未连接到终端将其用作重新加载"并不罕见:

SIGHUP is emitted when the controlling terminal is closed, but since most daemons are not attached to a terminal it is not uncommon to use it as "reload":

守护程序有时会使用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.

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

BTW, your watchdog could read a config file from time to time in order to know if it should relaunch the process.

我个人最喜欢看门狗的是主管.

My personal favorite for a watchdog is 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似乎是一个错误的选择.

See if kill -l returns the list of signals on your platform and try some of them, but SIGUSR1 seems like a bad choice.

$ 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之间行为差异的评论:

Carpetsmoker comments about differences in behavior between Linux and BSDs:

SIGINFO在GNU libc&上的工作方式似乎有所不同. BSD;在BSD上,它可以按您所描述的那样工作,但是在Linux上,它要么不存在,要么与SIGPWR相同... GNU libc手册在这方面似乎不正确(您的kill -l输出也没有显示SIGINFO )...我不知道为什么GNU不支持它,因为我发现它非常有用... – Carpetsmoker

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的情况下发信号通知应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆