简体   繁体   English

如何知道 Linux 上的信号来自哪里?

[英]How to know where does a signal come from on Linux?

I'm running a python script but it is always stopped somehow.我正在运行 python 脚本,但它总是以某种方式停止。 So I need to know who stopped this process.所以我需要知道是谁阻止了这个过程。 在此处输入图像描述

Is there any tool to know who sent the signal that stopped a process?是否有任何工具可以知道谁发送了停止进程的信号?

If you are able to wait at the end of your main process, you can add something like:如果您能够在主进程结束时等待,您可以添加如下内容:

import signal
siginfo = signal.sigwaitinfo({signal.SIGTERM})
print("got %d from %d by user %d\n" % (siginfo.si_signo,
                                         siginfo.si_pid,
                                         siginfo.si_uid))

(Adapted from here : works on Python 3.5.2 on Linux) (改编自此处:适用于 Linux 上的 Python 3.5.2)

which will block your script and make it wait until it gets a SIGTERM, then it'll print out the pid of the process that sent the SIGTERM.这将阻止你的脚本并让它等到它得到一个 SIGTERM,然后它会打印出发送 SIGTERM 的进程的 pid。 You can swap SIGTERM for SIGINT if it was a SIGINT that stopped your program.如果是 SIGINT 停止了您的程序,您可以将 SIGTERM 换成 SIGINT。 Unfortunately you can only catch signals in the main process and not in seperate threads, see here for more information.不幸的是,您只能在主进程中捕获信号,而不能在单独的线程中捕获信号,请参阅此处了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM