Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting STDOUT and STDERR to a file, except for standard output/error of system()

I am trying to run a Perl script in Linux and log all output to STDOUT and STDERR to a file using:

open (STDOUT, "| tee -i $transcript_file");
open (STDERR, "| tee -ai $transcript_file");

The script that uses this works roughly as follows:

  1. Create an environment for running a tool. Has many print, warn and possibly die statements.
  2. Run the tool (Currently using system command). This produces a lot of output which I want to appear on STDOUT, but not in the logfile (The tool creates its own logfile).
  3. Analyze the results, cleanup and exit. Has many print, warn and possibly die statements.

Everything works correctly except I would like to exclude the output of step 2 from the log. Is there a simple way to achieve this?

Thanks,

PS: This is my first question on stackoverflow. Please help me in asking questions correctly if I have not done so.

like image 394
Abhishek Avatar asked Dec 02 '25 08:12

Abhishek


1 Answers

I agree with Sobrique's advice to use a special function print_and_log. But if you really want to do it the way you set out to do, you can dup STDOUT and STDERR, redirect them to your log and then use open3 to run your tool with the dup'ed original standard output and error file descriptors

use  IPC::Open3;

# dup the old standard output and error 
open(OLDOUT, ">&STDOUT") or die "Can't dup STDOUT: $!\n";
open(OLDERR, ">&STDERR") or die "Can't dup STDERR: $!\n";

# reopen stdout and stderr
open (STDOUT, "|tee $transcript_file") or die "Can't reopen STDOUT: $!\n";
open (STDERR, ">&STDOUT")              or die "Can't reopen STDERR: $!\n";

# print statements now write to log
print "Logging important info: blah!\n";
print STDERR "OOPS!\n";

# run your command; output will go to original stdout
# this will die() instead of returning say -1, so use eval() to catch errors
my $pid = open3(">&OLDOUT", "<&STDIN", ">&OLDERR", $command); 

# wash those dishes....
waitpid( $pid, 0 );
like image 181
Hans Lub Avatar answered Dec 03 '25 22:12

Hans Lub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!