[英]How to know where does a signal come from on Linux?
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.