I've often found for what I'm using system() for that the implicit fork inherent in a open(CHILD, "|-") is a useful way to go. I get the fork plus control over the child's STDIN in one shot. I also get an automatic waitpid call when I close the parent side.
For example, culled from production code in a case where we don't want the child process to have any possible way to be still talking to the controlling terminal:
# my $status = system($command);
# Our better version of system()
my $status;
my $pid = open(KID_STDIN, "|-");
if (not defined $pid) {
die "cannot fork: $!; bailing out";
}
if ($pid) { ## parent
close(KID_STDIN);
$status = $?;
} else {
POSIX::setsid(); # disconnect from controlling terminal
open(STDOUT, ">> $_LogFileName");
open(STDERR, '>&STDOUT');
exec($command);
}
In the specific example here, we're using system to run java programs which read from data files given as parameters and produce log output on STDOUT, with exceptional output (such as an uncaught runtime exception that produces a stacktrace) going to STDERR. Because java plays games with its signal handlers, we discovered that running the driver perl script under "nohup" did not in fact allow us to start the driver script up and let it run in the background - java would still abort when the initial xterm was closed. Rewriting system() to include the setsid call finally fixed that problem.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.