Below code work but after executing command in fork child its returning commands exist value but parents is not getting childs correct exits value, either success or failed parents is always getting '0' as $? is which means success! Can anyone suggest me better way of doing this?
my $pid = fork(); if($pid == 0) { my $log = '/home/tart/tmp/log.log'; open(CLOG, ">>$log") || die "$0: can't open '$log': $!\n"; open(STDOUT, ">&CLOG")|| die "$0: Can't remap STDOUT: $!\n"; open(STDERR, ">&CLOG")|| die "$0: Can't remap STDERR: $!\n"; @systemOut = `$command`; print CLOG "@systemOut"; close CLOG; exit $?; } else { while (waitpid($pid, WNOHANG) == 0) { sleep 1; my $upTime = time; my $diffTime = $upTime - $sTime; $diffTime = int($diffTime/60); verbose($configHR, "Processing...since $diffTime Min!") if($diffTime != $regVal); $regVal = $diffTime; } } if($? == 0) { print LOG "Status: Success\n"; return 1; } print LOG "Status: Failed While Executing $command\n";
Thanks, -------Updates-------
Hi,

My Above listed fork and execution process is in the subroutine which is first evaluated by eval.
To be more specific, First eval evaluate the subroutine name '&execute($cmd)' then inside the execute subroutine fork runs and in child process commands get executed as in first posted code,
So, just wondering does eval does any thing in forking process? example,
#$sub is subroutine name. #for example: $sub = '&execute'. my $stmt = '$rv = '. $sub; eval $stmt; if($@) { print $@, "\n"; } sub execute { my $pid = fork(); if($pid == 0) { my $log = '/home/tart/tmp/log.log'; open(CLOG, ">>$log") || die "$0: can't open '$log': $!\n"; open(STDOUT, ">&CLOG")|| die "$0: Can't remap STDOUT: $!\n"; open(STDERR, ">&CLOG")|| die "$0: Can't remap STDERR: $!\n"; @systemOut = `$command`; print CLOG "@systemOut"; close CLOG; exit $?; } else { while (waitpid($pid, WNOHANG) == 0) { sleep 1; my $upTime = time; my $diffTime = $upTime - $sTime; $diffTime = int($diffTime/60); verbose($configHR, "Processing...since $diffTime Min!") if($diffTime != $regVal); $regVal = $diffTime; } } if($? == 0) { print LOG "Status: Success\n"; return 1; } print LOG "Status: Failed While Executing $command\n"; }
Thanks,

In reply to fork child exist value by tart

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.