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!