0

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?

Guddu
  • 1,588
  • 4
  • 25
  • 53
  • Can you provide a snippet of code of the shell script, or a little bit about what it does? In some cases, simply using ``exec program`` might solve that problem by eliminating the shell process altogether. – Rudy Matela Jun 26 '13 at 03:05
  • @RudyMatela Thanks for your input. Unfortunately the Launhcer Program written in C++ cannot be changed. I can change the script though. The script in itself nasically runs in a Infinite loop asking for parameters that I get from the user and querying a Oracle DB and fetching the result and showing to the user and continuing in the loop. I have pasted a snippet that might be of interest at http://dpaste.com/1271101/ This is only a part of the snippet and the loop does what I indicated above. – Guddu Jun 26 '13 at 03:49

2 Answers2

1

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
pedz
  • 2,271
  • 1
  • 17
  • 20
  • You summed it up really well. I am going to consider mapping CTRL-X to SIGINT. I will save the original term settings and restore it upon exit. I am off to trying this now in my test environment. – Guddu Jun 26 '13 at 14:43
  • Unfortunately $PPID always remains as the original parent process ID and does not change to 1 for a Orphaned process – Guddu Jun 26 '13 at 18:37
  • You are correct... That is most unfortunate. I put an edit in my original post – pedz Jun 26 '13 at 22:01
  • Hi pedz...Thanks for the update. I will try this now. Could u please tell me what kill -0 does. Is 0 a special signal? – Guddu Jun 27 '13 at 04:09
  • I may get the terminology wrong but kill -0 simply checks to see if the process exists. It does not send a signal. Now, what I don't know is the exact details of how it does it. But you can do it from C code too with the kill system call. http://stackoverflow.com/questions/11012527/what-does-kill-0-pid-in-a-shell-script-do – pedz Jun 27 '13 at 11:53
0

ksh always does this. Just got bitten by this, unlike bash, ksh does not forward hup signals when you exit. if you can find the child pids you can hup them yourself.

teknopaul
  • 6,505
  • 2
  • 30
  • 24