Great and wise monks,

I am fighting the dragon of windows and fear to say the great wyrm is winning. I need to have a parent process launch a child which will run detached from the parent. The parent should be able to carry on running once the child is launched. When the parent exits the child should carry on regardless.

I have a small bit of test code but I am getting odd behaviour...

#!/usr/bin/perl use strict; use warnings; $SIG{CHLD} = 'IGNORE'; $|++; if (@ARGV) { payload(@ARGV) } else { for (1..3) { my $pid = fork; die "failed to forking fork\n" unless defined $pid; if ($pid) { # parent, loop on ... } else { # child, run the payload print "I am a child\n"; `wperl $0 little_forker $_`; print "wperl launched\n"; } } } exit; sub payload { my ($type, $id) = @_; sleep 30; # ZZZzzzz..... open my $fh, '>>', "C:/tmp/$type.$id.log"; print $fh "number $id of type $type ran OK\n"; close $fh; exit; }
the three children are spawned and run on OK (the output files are written) even if CTRL-C out of the parent. The odd bit is that unless I CTRL-C the parent never exits, not even after the children are done. Also it only ever prints one line of output though the correct running of three children leads me to expect something more. Here I hit CTRL-C in the end about 30 secs after the children had finnished.
C:\Random>perl winproc.pl I am a child Terminating on signal SIGINT(2)
I am not getting a warm fuzzy feeling from this. How can I do it better ?

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to detached process in windows by Random_Walk

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.