Trap not working in orphaned child processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trap not working in orphaned child processes
# 1  
Old 01-15-2010
Question Trap not working in orphaned child processes

I've search the various posts in these forums, but have not come up with a solution to my problem.

I have a parent process that calls a child script, runs it in the background and the parent finishes - without waiting for the child process to complete. Inside the child, a trap is issued to trap the QUIT signal and set a flag so that the child process can finish its current loop of processing, detect the flag and break out of the loop gracefully.

The problem is that the trap is not working in the orphaned child process. This only happens when I call the child from a parent and then let the parent complete without waiting for the child process to complete. As long as the parent process is still running, the child will properly trap the signal.

I would like to find a way to change this behavior so that the child - orphaned from its parent - will still trap signals sent to it.

Here's a simplistic example of how to set this up:

parent.ksh
Code:
child.ksh &

child.ksh
Code:
#!/bin/ksh
QUIT_THE_PROGRAM=N
trap "QUIT_THE_PROGRAM=Y" QUIT  #here's the trap that is not working when orphaned
while [[ ${QUIT_THE_PROGRAM} = "N" ]]
do
   echo Child PID - $$  # let me see the pid of this child process 
   sleep 5
   echo QUIT_THE_PROGRAM - ${QUIT_THE_PROGRAM}
   if [[ ${QUIT_THE_PROGRAM} = "Y" ]]
   then
      break
   fi
done

Make sure you chmod a+x child.ksh parent.ksh to make them executable.


OK - now run parent.ksh. That will call child.ksh and run it in the background and then the parent will complete and effectively orphan the child. Every 5 seconds, the child process will show you it's PID and the value of the QUIT_THE_PROGRAM variable.

Now, issue a kill -QUIT <pid> where <pid> is the PID of the child process. The child process ignores the signal - or at least the trap ignores it.

This is the behavior I want to change. I want this child process to trap the QUIT and change the value of the QUIT_THE_PROGRAM variable to Y.


Now - you can remove the & from parent.ksh and then run parent.ksh. This time the parent is waiting for the child process to complete. If you open another session on the server, you can issue a kill -QUIT <pid> where <pid> is the PID of the child process that is being printed in your first session. Now you will see that the non-orphaned child behaves properly and traps the QUIT signal and changes the value of the QUIT_THE_PROGRAM variable to Y.


Any ideas on how I can get the orphaned child process to trap the signal???

Thanks in advance for your input!
# 2  
Old 01-15-2010
If I am getting what you are doing:

orphan processes are adopted by init, so that means you no longer own the process. [edit] init becomes the parent
[wrong]: You cannot signal a process you don't own. [/edit]

Have the parent signal the child before it exits. You do know that conventional program design in unix has the parent wait for the child, then exit - daemons excluded.

Last edited by jim mcnamara; 01-15-2010 at 02:44 PM.. Reason: Error
# 3  
Old 01-15-2010
Interesting - the ps output still shows my id as running this process - though the PPID is 1.

Let's assume that you're correct (which I am sure you are despite what I see in ps).

I need a way to signal this orphaned process that is now owned by init. Is there a way?

I even tried logging in as root and issuing the kill -QUIT <orphaned_pid> and the child process would not trap the QUIT signal.

My problem is this - even if I follow proper coding practices and have the parent wait, someone or something could accidentally kill the parent and then the child will still be running. Then I have no method to gracefully end the child - which is needed. (The child process runs db table reorgs that if killed mid-process could corrupt the table.)

So the question is - how do I signal an orphaned process?

I can come up with other ways to trigger the QUIT - for example: read a control file everytime through the loop and then manually update the control file with a QUIT to trigger the QUIT_THE_PROGRAM=Y. But I like the trap
# 4  
Old 01-15-2010
You are correct - the process owner is the original user. My mistake.
# 5  
Old 01-15-2010
"The INT and QUIT signals for an invoked command are ignored if the command is followed by & and the monitor option is not active." -- ksh(1)

Either use the monitor option (set -m) in parent.ksh or use a different signal (perhaps SIGTERM).

Cheers,
alister
# 6  
Old 01-15-2010
I am not a ksh expert, but I suppose that ksh is ignoring SIGQUIT for background processes. Accordingly to my system trace, your handler gets first installed, but then SIGQUIT sigaction gets reset to SIG_IGN.

If you use another signal, like SIGTERM, it seems that you can reach what you are looking for.

HTH,
Loïc.
# 7  
Old 01-15-2010
SOLVED!!!

alister wins the prize (for being first with the solution...)


Changing parent.ksh allows QUIT to be trapped by the child process.
Code:
set -m
child.ksh &

Also - even without set -m, trapping TERM also gets the desired results.

Thanks for the solutions!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get all child processes of a process

is there a universal way of getting the children of a particular process? i'm looking for a solution that works across different OSes...linux, aix, sunos, hpux. i did a search online and i kept finding answers that were specific to Linux..i.e. pstree. i want to be able to specify a process... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. UNIX for Dummies Questions & Answers

Orphaned processes

From my understanding orphaned processes imply the processes which are not actually running really.it might have already finished buy still showing the process Id Is there any command to filter out those orphaned processes alone? (2 Replies)
Discussion started by: pandeesh
2 Replies

3. Shell Programming and Scripting

Getting exit status of child in trap handler

Hi, I have a trap problem when calling a child script in the background. I know there are a lot of threads here on the issue of traps and signals, I think I have read all the relevant ones, but still haven't found an answer to my problem. I'm working on Linux or HP, the script as you can see... (4 Replies)
Discussion started by: rimon
4 Replies

4. UNIX for Advanced & Expert Users

Kill all child processes on trap

hi OS: Sun Solaris I have a scenario that when someone presses ctrl-c while executing a shell script, it should not just exit. it should kill all the child processes started by the running shell script only. I am executing many other scripts parallely which in turn fork off more... (2 Replies)
Discussion started by: rakeshou
2 Replies

5. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

6. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

7. Shell Programming and Scripting

Parent/Child Processes

Hello. I have a global function name func1() that I am sourcing in from script A. I call the function from script B. Is there a way to find out which script called func1() dynamically so that the func1() can report it in the event there are errors? Thanks (2 Replies)
Discussion started by: yoi2hot4ya
2 Replies

8. Shell Programming and Scripting

Over-riding TRAP in Child

Hi, I'm using Ksh on HP 10.2. My parent shell script has ignored INT signal using trap command. trap "" 2 3 .... (other signals) This script calls another script in which INT signal should be active and should not be ignored. I browsed the net and found out that in ksh, once a... (1 Reply)
Discussion started by: anijog
1 Replies

9. Programming

Controlling child processes

Hello all, I am trying to create n child processes and control them from a parent process; say make child 3 print its pid and then child 5 do the same and some other stuff. Is there a way to accomplishing this after all the child processes are created via a call to fork(). Thank you, FG (23 Replies)
Discussion started by: forumGuy
23 Replies

10. UNIX for Dummies Questions & Answers

what are parent and child processes all about?

I don't follow what these are... this is what my text says... "When a process is started, a duplicate of that process is created. This new process is called the child and the process that created it is called the parent. The child process then replaces the copy for the code the parent... (1 Reply)
Discussion started by: xyyz
1 Replies
Login or Register to Ask a Question