Why does the SIGCHLD generated by process continuation not activate the trap?
2
votes
1
answer
239
views
I am using linux (Ubuntu) and bash.
I made a simple Go program. Literally infinite for-loop that prints text every 20 seconds.
package main
import (
"fmt"
"time"
)
func main() {
for {
fmt.Println("Hi from program 1")
time.Sleep(time.Second * 20)
}
}
First, I register a trap for the SIGCHLD
signal using the command:
trap 'echo "hi you"' SIGCHLD
Next, I start a program asynchronously (in the background) with:
./program &
I started it as background job so bash can use terminal. When I execute the command:
kill -SIGSTOP process_id
the trap is triggered, and the message "hi you" is displayed as expected. However, when I run:
kill -SIGCONT process_id
to resume the process, the process continues running (printing "Hi from program 1"), but trap message ("hi you") is not displayed. I invoke these kill
commands from another terminal.
I found in [the bash documentation](https://www.gnu.org/software/bash/manual/html_node/Job-Control-Basics.html) the following:
> Any trap on SIGCHLD is executed for each child process that exits.
I could understand it as exclusive. In other words, that the SIGCHLD
trap is activated only when the child process exits. If so, it would be clear to me why my example does not work. However, the SIGCHLD
trap is also activated when the child process is suspended.
Why is this happening? Resuming the process also generates a SIGCHLD
signal, so why doesn’t the trap trigger in this case?
---
Related [question](https://unix.stackexchange.com/questions/790116/why-does-the-linux-manual-say-nothing-about-generating-a-sigchld-signal-when-the) .
Asked by Yakog
(517 rep)
Jan 25, 2025, 08:44 AM
Last activity: Jan 25, 2025, 07:39 PM
Last activity: Jan 25, 2025, 07:39 PM