简体   繁体   中英

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

I'm running a python script but it is always stopped somehow. 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)

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. You can swap SIGTERM for SIGINT if it was a SIGINT that stopped your program. Unfortunately you can only catch signals in the main process and not in seperate threads, see here for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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