2

I would like to redirect both stdout and stderr to file1, but also redirect only stderr to file2. Is this possible? I have a working script for part 1, but can't figure out how to additionally write stderr to file2, without overwriting the redirection to file1 (in actuality, the first stderr redirect is going to stdout which then goes to file1). I'm trying to do this so file1 can contain print statements and errors as it would if I were running in a console, and file2 will just log the errors.

Any idea on the best way to achieve this without straying too much from what I already have?

open STDOUT, '>', "$file1" or die $!; # redirect stdout to file
open STDERR, '>&STDOUT' or die $!; # redirect standard error to stdout
# now how to *also* redirect STDERR to $file2?

Thanks

Edit: I do not believe this is a duplicate of this question; it it is, I'm missing it. They wanted to get the output to the screen and into a logfile, I want my stdout/stderr to go to the same file in addition to only stderr going to another file of its own. I don't want (or can't see) any output on the screen. This is like my 3rd time writing in perl so it's pretty new to me. I've been looking at this for hours (including already looking at the 'duplicate' post you linked), and can't find a reasonable solution to my problem.

Community
  • 1
  • 1
user20408
  • 679
  • 2
  • 7
  • 23
  • I would take a look at [`Capture::Tiny`](https://metacpan.org/pod/Capture::Tiny#USAGE) – Hunter McMillen Feb 13 '15 at 20:51
  • Someone want to explain the down-vote? – user20408 Feb 13 '15 at 20:51
  • @user20408: SO is a mean place, you can get downvote-and-run here... Anyway, while your question is not quite a dupe, maybe it's a dupe when you consider the answers to the pseudo-dupe. Am casting a reopen vote but please have a look at them. – einpoklum Feb 13 '15 at 22:11
  • You're most of the way there. You've successfully redirected `STDERR` to `STDOUT`; the only thing left is to also send `STDERR` to a file. The first answer in the linked duplicate shows you how to make a filehandle write to more than one file at a time, which is exactly what you want to do: `*STDERR->autoflush; *STDERR->push_layer(tee => '>>file2');` – ThisSuitIsBlackNot Feb 13 '15 at 22:56
  • ThisSuit, thanks for the suggestion. The only problem is that I'm using perl on a cloud server, and have no access to the system. I'm getting the error: "Can't locate PerlIO/Util.pm in @INC," which I'm assuming means I don't have that module. Can you think of anything else that might work? Either way, I really appreciate it. – user20408 Feb 26 '15 at 20:35
  • @user20408 (FYI, you can use `@username` to notify users that you're replying to them.) I'm not sure what you mean by "have no access to the system." If that's the case, how are you running your script? If you mean "I don't have admin rights," you should still be able to [install modules with `local::lib`](http://stackoverflow.com/questions/3735836/how-can-i-install-perl-modules-without-root-privileges). – ThisSuitIsBlackNot Mar 02 '15 at 20:56

1 Answers1

3

You could try the Perl Module IO::Tee which allows you to redirect output to more than one output handle.

OtherDevOpsGene
  • 7,302
  • 2
  • 31
  • 46
  • I saw tee everywhere when I was doing my search, so I did look into it. I still couldn't figure out how to redirect stderr to both stdout (and then send those to file1), and file2. I could only find out how to redirect stderr and stdout to the same file, or redirect stdout to both the console and a file. I've been looking for a while now, and it's just not clear to me. Thanks. – user20408 Feb 13 '15 at 21:09