Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I receive notification in a bash script when a specific child process terminates?

I wonder if anyone can help with this?

I have a bash script. It starts a sub-process which is another gui-based application. The bash script then goes into an interactive mode getting input from the user. This interactive mode continues indefinately. I would like it to terminate when the gui-application in the sub-process exits.

I have looked at SIGCHLD but this doesn't seem to be the answer. Here's what I've tried but I don't get a signal when the prog ends.

set -o monitor

"${prog}" &
prog_pid=$!

function check_pid {
    kill -0 $1 2> /dev/null
}

function cleanup {
    ### does cleanup stuff here
    exit
}


function sigchld {
    check_pid $prog_pid
    [[ $? == 1 ]] && cleanup
}

trap sigchld SIGCHLD

Updated following answers. I now have this working using the suggestion from 'nosid'. I have another, related, issue now which is that the interactive process that follows is a basic menu driven process that blocks waiting for key input from the user. If the child process ends the USR1 signal is not handled until after input is received. Is there any way to force the signal to be handled immediately?

The wait look looks like this:

stty raw                 # set the tty driver to raw mode 
max=$1                   # maximum valid choice
choice=$(expr $max + 1)  # invalid choice
while [[ $choice -gt $max ]]; do
    choice=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
done
stty sane                # restore tty

Updated with solution. I have solved this. The trick was to use nonblocking I/O for the read. Now, with the answer from 'nosid' and my modifications, I have exactly what I want. For completeness, here is what works for me:

#!/bin/bash -bm
{
"${1}"
kill -USR1 $$
} &

function cleanup {
    # cleanup stuff
    exit
}

trap cleanup SIGUSR1

while true ; do
   stty raw                 # set the tty driver to raw mode 
   max=9                    # maximum valid choice
   while [[ $choice -gt $max || -z $choice ]]; do
       choice=`dd iflag=nonblock if=/dev/tty bs=1 count=1 2>/dev/null`
   done
   stty sane                # restore tty

   # process choice       

done
like image 848
starfry Avatar asked Feb 19 '23 23:02

starfry


1 Answers

Here is a different approach. Instead of using SIGCHLD, you can execute an arbitrary command as soon as the GUI application terminates.

{
    some_command args...
    kill -USR1 $$
} &

function sigusr1() { ... }

trap sigusr1 SIGUSR1
like image 88
nosid Avatar answered Apr 28 '23 08:04

nosid