Can a Linux-process intercept signals sent to its child?
5
votes
1
answer
1311
views
I have a shell-wrapper around a large executable. It does something like this:
run/the/real/executable "$@" &
PID=$!
# perform
# a few
# minor things
wait $PID
# perform some
# post-processing
One of the things it does after the
wait
is check for core-dumps and handle the crashes, however, by then the process is already dead and some information no longer available.
Can the fatal signal (SIGSEGV
or SIGBUS
) be intercepted by the shell script before it is delivered to the child itself?
I'd then be able to, for example, perform lsof -p $PID
to get the list of files opened by the wrapped process before it dies...
_Update_: I tried using strace
to catch the process receiving a signal. Unfortunately, there seems to be a race -- when strace
reports the child's signal, the child is on its way out and there is no telling, whether the lsof
will get the list of its files or not...
Here is the test script, which spawns off /bin/sleep
and tries to get the files it has opened for writing. Some times the /tmp/sleep-output.txt
is reported as it should be, other times the list is empty...
ulimit -c 0
/bin/sleep 15 > /tmp/sleep-output.txt &
NPID=$!
echo "Me: $$, sleep: $NPID"
(sleep 3; kill -BUS $NPID) &
ps -ww $NPID
while read line
do
set -x
outputfiles=$(lsof -F an -b -w -p $NPID | sed -n '/^aw$/ {n; s,.,,; p}')
ps -ww $NPID
lsof -F an -b -w -p $NPID
break
done &1)
echo $outputfiles
wait $NPID
The above test requires use of ksh
or bash
(for the < <(...)
construct to work).
Asked by Mikhail T.
(864 rep)
Jun 25, 2018, 06:32 PM
Last activity: Oct 14, 2024, 02:11 AM
Last activity: Oct 14, 2024, 02:11 AM