Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: fork and stdout/stderr

by isotope (Deacon)
on Feb 25, 2003 at 05:26 UTC ( [id://238359]=note: print w/replies, xml ) Need Help??


in reply to fork and stdout/stderr

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/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://238359]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found