Linux 什么会导致 SIGHUP 生成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13337701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:45:43  来源:igfitidea点击:

What can cause SIGHUP to be generated?

linuxubuntulinux-kernel

提问by Peter

We have about 40 computers running identical hardware and software. They all run Ubuntu 11.10. They all have just one user account to log in. The .profile file is set up to launch a daemon process. The code for the daemon is written in C.

我们有大约 40 台计算机运行相同的硬件和软件。他们都运行 Ubuntu 11.10。他们都只有一个用户帐户可以登录。.profile 文件被设置为启动一个守护进程。守护进程的代码是用 C 编写的。

Once in a few weeks, we get a report that the daemon is no longer running. This does not happen on all computers but just one or two. We cannot reproduce the problem consistently.

每隔几周,我们就会收到一个报告,指出守护程序不再运行。这不会发生在所有计算机上,而只会发生在一两台计算机上。我们无法始终如一地重现该问题。

Looking at the code, the application quits when it receives either SIGHUP or SIGTERM.

查看代码,应用程序在收到 SIGHUP 或 SIGTERM 时退出。

As I understand, SIGHUP is generated when a user logs off. In our case, the user never logs off. I am wondering if it is possible that SIGHUP could have been generated for some other reason. Any other thought would be appreciated.

据我了解,当用户注销时会生成 SIGHUP。在我们的例子中,用户永远不会注销。我想知道是否有可能由于其他原因生成了 SIGHUP。任何其他想法将不胜感激。

采纳答案by mvp

Signals can be sent using killutility or kill syscall. Of course, you can try and find out who is sending that signal or disconnecting your terminals or network connections, but there is simpler practical way to fix your problem.

可以使用kill实用程序或kill syscall发送信号。当然,您可以尝试找出谁在发送该信号或断开您的终端或网络连接,但有更简单实用的方法来解决您的问题。

When code is supposed to run as a daemon, but really isn't (just like yours), there is a wrapper that can turn any program into daemon. Surprise - this wrapper is called daemon! It has lots of options, probably most importantly for you, option to automatically restart your utility should it ever die for any reason.

当代码应该作为守护进程运行,但实际上不是(就像你的一样)时,有一个包装器可以将任何程序变成守护进程。惊喜 - 这个包装被称为daemon!它有很多选项,可能对您来说最重要的是,如果您的实用程序因任何原因死亡,则可以选择自动重新启动它。

If this command is not installed on your Ubuntu, just sudo apt-get install daemon, and man daemonto get started.

如果您的 Ubuntu 上未安装此命令,只需sudo apt-get install daemon, 和man daemon开始。

回答by stsquad

Well, there are a couple of things to note about SIGHUP. Firstly, its origin is from the concept of a hang-up, i.e. loss of connection to a console over something like a modem. In modern parlance this generally means it has lost its controlling tty. Unless you've taken care to detach from your tty, any program started in a given terminal will receive a SIGHUP when the terminal is closed. See herefor details on how to do this in your program. Other options include:

嗯,关于 SIGHUP 有几点需要注意。首先,它起源于挂断的概念,即通过调制解调器之类的东西失去与控制台的连接。用现代的话来说,这通常意味着它已经失去了控制权。除非您小心地与 tty 分离,否则在给定终端中启动的任何程序都会在终端关闭时收到 SIGHUP。有关如何在您的程序中执行此操作的详细信息,请参见此处。其他选项包括:

  • running your program inside screenor tmux
  • run your program with nohupor some other daemonising framework
  • 在里面运行你的程序screen或者tmux
  • 使用nohup或其他一些守护程序框架运行您的程序

The other possibility is something is deliberately sending your process a SIGHUP which by "tradition" is often used to signal a process that it should re-read its configuration.

另一种可能性是故意向您的进程发送一个 SIGHUP,按照“传统”,它通常用于向进程发出信号,表明它应该重新读取其配置。