in reply to Starting a process in the background that lives after perl dies.

Use a combination of fork() and exec()
#!/usr/bin/perl -w use strict; my $pid = fork(); if ($pid == 0) { # This is the child process. # exec() the external program. exec("my_program") or die "could not exec my_program: $!"; } elsif (!defined($pid)) { die "could not fork"; } # Everything below here is the parent process print "This is the parent process\n";

-Matt

  • Comment on Re: Starting a process in the background that lives after perl dies.
  • Download Code

Replies are listed 'Best First'.
Re: Re: Starting a process in the background that lives after perl dies.
by ehdonhon (Curate) on Nov 08, 2001 at 23:43 UTC
    Thanks,

    Unfortunately that doesn't work. As soon as the main program (the one that did the forking) finishes running, the child process (the one that got forked) gets reaped.

    I even tried modifying that code slightly to incorporate the previous suggestion of using Proc::Daemon::init. It doesn't matter, the child still dies. Is it possible that this is simply something that can only be done in sh and not in Perl?

    Here is my test code (based on your suggestion):
    a.pl :

    #!/usr/local/bin/perl my $pid = fork(); if ($pid == 0) { # This is the child process. # exec() the external program. exec("./b.pl &") or die "could not exec my_program: $!"; } elsif (!defined($pid)) { die "could not fork"; } # Everything below here is the parent process print "This is the parent process\n";
    b.pl:
    #!/usr/local/bin/perl use Proc::Daemon; Proc::Daemon::init; open ( OUT, ">b.out" ); while ( 1 ) { print OUT `/bin/date`; sleep( 2 ); }

    I found that a.pl is calling b.pl, which is outputing the present time to b.out. But then, a.pl exits out, and so does b.pl. This is not the desired result. By the way, I'm doing this on FreeBSD 4.1.1 if that makes any difference.

      Here's the test code I used:
      #!/usr/bin/perl -w # forker.pl use strict; my $pid = fork(); if ($pid == 0) { exec("/tmp/busy_wait.pl"); } elsif (!defined($pid)) { die "could not fork"; } print "This is the parent process\n";
      #!/usr/bin/perl -l # busy_wait.pl while (1) { print "Busy!"; sleep 2; }
      When I execute forker.pl, I get this output:
      [matt@megatron ~]$ /tmp/forker.pl Busy! This is the parent process [matt@megatron ~]$ Busy! Busy! Busy!
      The parent dies, and I get my shell prompt back, but the busy_wait.pl child lives on until I purposefully kill it.

      -Matt

        Hi, thank you

        You were absolutely right.. that does work just like you said.

        Here's an element of the puzzle that I left out, and this is where I was getting caught.. this entire thing is running through xinetd. And even though the parent was dieing off, the child still was keeping the standard file discriptors open. As a result, my client that was waiting for an EOF on the socket was never getting it. DOH!

        So when I took what I learned from here and the example code and then applied it directly into the real code it worked perfectly as soon as I let my child run Proc::Daemon::Init, which closed the file handles for me.

        Thank you everybody for helping me figgure this one out!