Shell – What’s the easiest way to detect what signals are being sent to a process

fishosxprocessshellsignals

I'm trying to debug an issue with my shell (specifically, fish) sending signals to my background processes. I'd like to be able to recognize what signals a process is receiving.

Ideally, I'd like some program that would do something like this:

$ log_signals > signals.txt &

Which would then write the signals it received to its output. However, I don't believe any such program readily exists.

What's the simplest way to trap incoming signals and inspect them, preferably without needing to write my own program?

(I'm running OS X, not Linux, so I'd prefer a more platform-agnostic answer if possible.)

Best Answer

Since Stéphane posted this information as a comment, but I found this information to be more useful than any existing answers, I'm reposting the comment as an answer.

For Linux only: strace prints signals by default, so you can use the -e flag to silence all system calls so that signals are clearer:

strace -e 'trace=!all' cmd

In my case, I was trying to figure out what process was killing a daemon I was running, so I got output that looks like this:

[hendrenj@underling02 ~]$ strace -p 171869 -e 'trace=!all'
Process 171869 attached
--- SIGTERM {si_signo=SIGTERM, si_code=SI_USER, si_pid=151513, si_uid=1000} ---
+++ killed by SIGTERM +++

Here you can see that I attached to PID 171869, and that process was sent SIGTERM by the process with PID 151513.

For further reference, you can check out this blog post, which suggests tools such as auditd (which I believe comes with SELinux if you are on a distro that uses it) or stap (System Tap) if strace isn't powerful enough.

Related Question