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
child.ksh
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???
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
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
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)