2

I'm running a python script but it is always stopped somehow. So I need to know who stopped this process. enter image description here

Is there any tool to know who sent the signal that stopped a process?

潇洒张
  • 273
  • 2
  • 9

1 Answers1

0

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.

secret squirrel
  • 802
  • 9
  • 13
  • I tried but the program couldn't terminate. I guess it is not a SIGTERM because the return value of the stopped python script is 149. I have never seen 149 as an return value before. – 潇洒张 Jul 30 '20 at 14:50
  • If the python script was stopped by a signal, it wouldnt neccessarily exit with a return code; it may get an exit status from the OS, which is always greater than 128 on linux. It could be 149 = 128 + signal number, see this: https://unix.stackexchange.com/a/99143 – secret squirrel Jul 30 '20 at 15:20