Ack! Why does everybody result to shell voodoo to do this? You can do the job in pure Perl. Seems like I had
some fun with this a long time ago.
#!/usr/bin/perl -wT
use strict;
my $pid1=fork();
unless($pid1) {
# First child
my $stdout_file = 'child1_stdout.log';
my $stderr_file = 'child1_stderr.log';
local(*STDERR);
local(*STDOUT);
open(STDOUT1, '>'.$stdout_file)
or die $stdout_file . ': ' . $!;
open(STDERR1, '>'.$stderr_file)
or die $stderr_file . ': ' . $!;
open(STDOUT, ">&STDOUT1")
or die "Couldn't redir stdout: $!";
open(STDERR, ">&STDERR1")
or die "Couldn't redir stderr: $!";
system('cmd1');
close(STDOUT1);
close(STDERR1);
exit();
}
my $pid2=fork();
unless($pid2) {
# Same thing but call cmd2 instead of cmd1
exit();
}
# Don't forget to wait for each child
wait();
wait();
Update: (sigh) Ok, the STDOUT/STDERR redirection won't carry out to the system() call. If you weren't calling an external program, this would work. Using
IPC::Open3 is probably the way to go. Using sockets to pass the data back to the parent will probably work better than anything involving the filesystem.
--isotope
http://www.skylab.org/~isotope/