Orphaning processes 
Author Message
 Orphaning processes

Hi,
        I'm trying to get a parent script to start a child script and then
completely ignore it (if the child script dies, faulters, seg faults I
don't care) on unix.  I started off with a simple forking script:
 #!/usr/local/bin/perl -w


if ($pid = fork) {
    #parent process

Quote:
} elsif (defined $pid) {

    #child process
        open(STDOUT,">/dev/null");
        open(STDERR,">&STDOUT");
        exec $command;
Quote:
} elsif ($! =~ /No more process/) {

        die ("Too many processes\n");
Quote:
} else {

        die ("Weird fork error\n");

Quote:
}

and even went to a double fork (basically I used this script twice,
version 1 calling version 2 calling the command), but still my parent
script gets hung up when something bad happens to it child.  Does
anybody have any suggestions?
        Thanks,
          Charles Plager

p.s. Please respond by email (as well as posting).
--
------------------------------------------------------------------

Dept of Physics, UIUC                 http://www.*-*-*.com/ ~cplager
office: (1) 607-255-7505                     fax: (1) 607-255-8062
------------------------------------------------------------------



Thu, 27 Feb 2003 22:58:51 GMT  
 Orphaning processes

 > Hi,
 >       I'm trying to get a parent script to start a child script and then
 > completely ignore it (if the child script dies, faulters, seg faults I
 > don't care) on unix.  I started off with a simple forking script:
...
 > but still my parent
 > script gets hung up when something bad happens to it child.  Does
 > anybody have any suggestions?

Have you tried closing STDIN (in addition to STDOUT and STDERR in your
example code)?  See perlfaq8 (How do I fork a daemon process?).  It
describes what your *children* should do.  Also, don't forget to have your
parent do

         $SIG{CHLD} = 'IGNORE';

before forking.  This will save you from the unreliable signal delivery
problem in perl and (at least in Solaris) will not allow zombies to
accumulate.

-Garry Williams



Sat, 01 Mar 2003 12:13:56 GMT  
 Orphaning processes


Quote:

> > Hi,
> >       I'm trying to get a parent script to start a child script and then
> > completely ignore it (if the child script dies, faulters, seg faults I
> > don't care) on unix.  I started off with a simple forking script:
>...
> > but still my parent
> > script gets hung up when something bad happens to it child.  Does
> > anybody have any suggestions?

>Have you tried closing STDIN (in addition to STDOUT and STDERR in your
>example code)?  See perlfaq8 (How do I fork a daemon process?).  It
>describes what your *children* should do.  Also, don't forget to have your
>parent do

>         $SIG{CHLD} = 'IGNORE';

>before forking.  This will save you from the unreliable signal delivery
>problem in perl and (at least in Solaris) will not allow zombies to
>accumulate.

1) Change default directory to / or /tmp or /var/tmp.
2) Close STDIN, STDOUT, STDERR.  Re-open STDIN to read from /dev/null,
   re-open STDERR and STDOUT to output to /dev/null or to a log file.
3) Fork a short-lived child that just forks a grandchild and exists.
4) Wait for short-lived child to exit.
   This leaves the grandchild parentless; it gets grafted on to pid 1.
5) Continue processing, ignoring the grandchild completely.
        -Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.


Mon, 03 Mar 2003 03:00:00 GMT  
 Orphaning processes

This is basically what I do, but haven't thought about it in a while.

Quote:
> 1) Change default directory to / or /tmp or /var/tmp.

So in what cases is that needed?  Only if you want to unmount some file
system after starting the new process?

Quote:
> 2) Close STDIN, STDOUT, STDERR.  Re-open STDIN to read from /dev/null,
>    re-open STDERR and STDOUT to output to /dev/null or to a log file.

Do you need to close and reopen?  Won't just opening to /dev/null be
enough?

Quote:
> 3) Fork a short-lived child that just forks a grandchild and exists.
> 4) Wait for short-lived child to exit.
>    This leaves the grandchild parentless; it gets grafted on to pid 1.

This is a double fork?  I've seen this used before, but I've never
understood why.  I just fork the child then exit the parent.  The parent
doesn't wait and the child is inherited by init.

I also setsid() in the child process.  I've never been clear on exactly
what this does.  Can someone explain?

--
Bill Moseley



Mon, 03 Mar 2003 03:00:00 GMT  
 Orphaning processes


Quote:

>This is basically what I do, but haven't thought about it in a while.

>> 1) Change default directory to / or /tmp or /var/tmp.

>So in what cases is that needed?  Only if you want to unmount some file
>system after starting the new process?

To cover the case that bit me once before: daemon died, su to root,
restart daemon while in someone's home directory, automounter not able
to unmount that home directory.

Quote:
>> 3) Fork a short-lived child that just forks a grandchild and exists.
>> 4) Wait for short-lived child to exit.
>>    This leaves the grandchild parentless; it gets grafted on to pid 1.

>This is a double fork?  I've seen this used before, but I've never
>understood why.  I just fork the child then exit the parent.  The parent
>doesn't wait and the child is inherited by init.

The original poster wanted the parent to stick around; it had other
things to do other than firing off a daemon child.

Quote:
>I also setsid() in the child process.  I've never been clear on exactly
>what this does.  Can someone explain?

If you have several processes associated with each other (such as when
a pipe with two or more processes), and you type a character that creates
a signal (^C, ^\, ^Z), then the signal is sent to all the process that
have the same session id.  setsid() is one step in divorcing the process
from its controlling terminal.
        -Joe

--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.



Tue, 04 Mar 2003 08:50:27 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. passing row position from dbgrid onto the next form

2. Orphaning processes

3. Need a HP ov Orphan Process Killer

4. format - the orphan child?

5. Spawn a process from CGI - server waits for orphaned child!

6. RTF processing or general text processing module

7. Perl process as a unix background process

8. How to capture process output by Win32::Process::Create

9. interactive process processing

10. Child processes keep appearing in process list

11. to get the process ID of a running process

12. Forking processes (getting child process id)

 

 
Powered by phpBB® Forum Software