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.


In reply to Re: Re: Starting a process in the background that lives after perl dies. by ehdonhon
in thread Starting a process in the background that lives after perl dies. by ehdonhon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.