Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get pid of the process which has triggered some signal

Tags:

python

signals

Is it possible to find out the process id of the process which has caused some signal. In my scenario, I have multiple children of a process running, and I want to know which one of them sent the signal.

like image 887
Nirmal Agarwal Avatar asked Feb 11 '11 21:02

Nirmal Agarwal


People also ask

How do you find the PID of a process that sent signal?

To know the PID of the process which sent a signal, set options=SA_SIGINFO , and use a sa_sigaction callback instead of sa_handler ; it will receive a siginfo_t struct, having a si_pid field. You can associate a data to the signal using sigqueue .

How get PID details in Linux?

In this quick article, we've explored how to get the name and the command line of a given PID in the Linux command line. The ps -p <PID> command is pretty straightforward to get the process information of a PID. Alternatively, we can also access the special /proc/PID directory to retrieve process information.

How check if process is running PID Linux?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.


1 Answers

It is (finally!) very simple with python 3.

The following is tested with python 3.3.3:

#! /usr/bin/python3

import signal
import time, os

def callme(num, frame):
    pass

# register the callback:
signal.signal(signal.SIGUSR1, callme)

print("py: Hi, I'm %d, talk to me with 'kill -SIGUSR1 %d'"
      % (os.getpid(),os.getpid()))

# wait for signal info:
while True:
    siginfo = signal.sigwaitinfo({signal.SIGUSR1})
    print("py: got %d from %d by user %d\n" % (siginfo.si_signo,
                                             siginfo.si_pid,
                                             siginfo.si_uid))
like image 194
Pietro Battiston Avatar answered Oct 26 '22 20:10

Pietro Battiston