我的应用程序在Linux上作为后台进程运行。它目前在终端窗口的命令行中启动。

最近,一个用户正在执行应用程序一段时间,它神秘地死亡了。文本:

杀了

在终端机上。这样发生了两次。我问是否有人在不同的终端使用kill命令杀死进程?不。

在什么情况下Linux会决定终止我的进程?我相信shell显示“已杀”是因为进程在接收到kill(9)信号后死亡。如果Linux发送了终止信号,系统日志中是否应该有一条消息解释为什么它被终止?


当前回答

PAM模块限制资源导致了您所描述的结果:我的进程神秘地死亡,控制台窗口上的文本为Killed。没有日志输出,无论是syslog还是kern.log。顶部程序帮助我发现,在CPU使用一分钟后,我的进程就会被杀死。

其他回答

PAM模块限制资源导致了您所描述的结果:我的进程神秘地死亡,控制台窗口上的文本为Killed。没有日志输出,无论是syslog还是kern.log。顶部程序帮助我发现,在CPU使用一分钟后,我的进程就会被杀死。

用户可以使用kill或Control+C杀死自己的程序,但我的印象是这并没有发生,而且用户向您投诉了。

Root当然有杀死程序的能力,但如果有人在你的机器上安装了Root并杀死了一些东西,你就有更大的问题了。

如果您不是系统管理员,系统管理员可能对CPU、RAM、磁盘使用设置了配额,并且会自动杀死超过配额的进程。

除了这些猜测,没有更多关于这个项目的信息,我不能确定。

像systemtap(或跟踪程序)这样的工具可以监视内核信号传输逻辑和报告。例如,https://sourceware.org/systemtap/examples/process/sigmon.stp

# stap --example sigmon.stp -x 31994 SIGKILL
   SPID     SNAME            RPID  RNAME            SIGNUM SIGNAME
   5609     bash             31994 find             9      SIGKILL

该脚本中的过滤if块可以根据口味进行调整,也可以取消以跟踪系统范围的信号流量。可以通过收集回溯跟踪来进一步隔离原因(分别为内核和用户空间向探针添加print_backtrace()和/或print_ubacktrace())。

这看起来是一篇关于这个主题的好文章:驯服OOM杀手(1)。

The gist is that Linux overcommits memory. When a process asks for more space, Linux will give it that space, even if it is claimed by another process, under the assumption that nobody actually uses all of the memory they ask for. The process will get exclusive use of the memory it has allocated when it actually uses it, not when it asks for it. This makes allocation quick, and might allow you to "cheat" and allocate more memory than you really have. However, once processes start using this memory, Linux might realize that it has been too generous in allocating memory it doesn't have, and will have to kill off a process to free some up. The process to be killed is based on a score taking into account runtime (long-running processes are safer), memory usage (greedy processes are less safe), and a few other factors, including a value you can adjust to make a process less likely to be killed. It's all described in the article in a lot more detail.

编辑:下面是[另一篇文章](2),它很好地解释了如何选择进程(带有一些内核代码示例的注释)。它的优点在于,它包含了一些关于各种bad()规则背后原因的注释。

如果用户或系统管理员没有杀死内核可能已经杀死的程序。内核只会在特殊情况下终止进程,比如资源极度匮乏(比如mem+swap耗尽)。