Search code examples
bashshellkshaix

Orphan Child (ksh Shell Script not terminating first upon CTRL-X)


I have a program (C++ Executable) on AIX 5.3 that launches a Shell Script (ksh).

When I launch the program and the shell script, i see two processes

AIX:>ps -ef | grep 3657892
u001 **3657892** 3670248   0 18:16:34 pts/11  0:00 /u0012006/bin/Launcher
u001 3723398 **3657892**   0 18:16:41 pts/11  0:00 /usr/bin/ksh /u0012006/shell/Trjt_Slds.sh -m

Now, When I do a CTRL-X key combination on the Keyboard to end and go out of the Shell Script, the main launching program (C++ Executable) process gets killed while the shell script continues to execute.

AIX:>ps -ef | grep 3723398    
u001 3723398       1 106 18:16:41 pts/11  0:01 /usr/bin/ksh /u0012006/shell/Trjt_Slds.sh -m
u001 3731504 3723398   0                  0:00 <defunct>
u001 3735612 3723398   0                  0:00 <defunct>
u001 3739838 3723398   0                  0:00 <defunct>

This is leading to the CPU Consumption going to 100% and a lot of defunct processes get launched.

Is there a way to have the AIX Shell Script terminate first when I do a CTRL-X?


Solution

  • Note: Launcher is broken and should be fixed. Thus, any "solution" will be a hack.

    One thought is to check $PPID in various places in the script. If it is set to 1 (init), then exit the script.

    I don't understand the use of control-X. That is not going to generate any tty signal. I guess that is what you want. Perhaps the tty is also in raw mode. But you might consider hooking control-X up to one of the various tty signals like SIGINT. e.g. stty intr ^X but you will also need to remember to unset it with stty intr ^C

    Last, you could wrap the script in a script and use the technique to kill the child and exit. e.g. (untested)

    #!/bin/ksh
    # launch original program in background
    /path/to/real/program "$@" &
    # get child's pid
    child=$!
    
    while : ; do
      # when we become an orphan
      if [[ $$PPID -eq 1 ]] ; then
        # kill the child and exit
        kill $child
        exit
      fi
      # poll once a second
      sleep 1
    done
    

    Update

    ./s1 is:

    #!/bin/ksh
    
    ./s2 &
    sleep 10
    exit
    

    ./s2 is:

    #!/bin/ksh
    
    while : ; do
      if kill -0 $PPID ; then
        echo still good
      else
        echo orphaned
        exit
      fi
      sleep 1
    done