3

I need to detect when one of my background processes exits. Hence, I installed a trap. run_gui and run_ai1 are simple exec functions.

run_gui & gui_pid=$!
run_ai1 & ai1_pid=$!
trap 'echo foo' SIGCHLD
while true; do
    echo "Started the loop"
    while true; do
        read -u $ai1_outfd line || echo "Nothing read"
        if [[ $line ]]; then
            : # Handle this
        fi
    done
    while true; do
        read -u $gui_outfd line || echo "nothing read"
        if [[ $line ]]; then
            : # Handle this
        fi
    done
done

When I close the GUI, nothing happens. The echo foo command is executed only if I press ctrl+c.

Why do I miss the SIGCHLD?

Jolta
  • 2,620
  • 1
  • 29
  • 42
marmistrz
  • 5,974
  • 10
  • 42
  • 94

1 Answers1

5

Non-interactive shells don't enable job control by default. See if putting 'set -o monitor' at the beginning of the script produces the results you want.

Source: Chet Ramey, GNU Bash's maintainer

Cyrus
  • 84,225
  • 14
  • 89
  • 153