Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

STDERR and STDOUT redirection from codeopen/code

by dpatrick (Scribe)
on Apr 11, 2002 at 19:50 UTC ( #158409=perlquestion: print w/replies, xml ) Need Help??

dpatrick has asked for the wisdom of the Perl Monks concerning the following question:

Sistren and Brethren, Is there a way for me to redirect STDIN and STDERR from a program that I'm running from within a script back into the script. Below is a snippet of a wrapper script that runs a couple of other scripts. This wrapper runs from a cron job and I'd like to be able to log the other script's output to a file for later viewing. It works right now, but only for STDOUT (though I suppose I could redirect STDERR to STDOUT in the other script).
{ # import PO data my $poImportDataOutErr; open (PO_IMPORT_DATA, '/usr2/web/tpm-secure/data_import/poImpo +rtData.pl |'); while(<PO_IMPORT_DATA>) { $poImportDataOutErr .= $_; } close (PO_IMPORT_DATA); open(DIL, DATA_IMPORT_LOG); print DIL $poImportDataOutErr; close(DIL); }


dpatrick
- I think scsh is cool.
Open Sourceror, Perlmonk
http://perlmonk.org/~dpatrick

Replies are listed 'Best First'.
Re: STDERR and STDOUT redirection from codeopen/code
by tachyon (Chancellor) on Apr 11, 2002 at 20:13 UTC

    This will do the trick for you (on *nix):

    my $command = 'ls -al'; open CMD, "$command 2>&1|" or die $!; my @output = <CMD>; close CMD;

    The open executes $command. The 2>&1 redirects STDERR to STDOUT and backgrounds process. The | pipes the output to the CMD filehandle which we then read from as usual.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks. That's exactly what I was looking for. This is what we had in the cron job but I wasn't sure whether I could do the same from Perl.

      dpatrick
      - I think scsh is cool.
      Open Sourceror, Perlmonk
      http://perlmonk.org/~dpatrick
Re: STDERR and STDOUT redirection from codeopen/code
by perlplexer (Hermit) on Apr 11, 2002 at 20:12 UTC
    Perhaps I misunderstood what you are trying to do there but if it's just a matter of redirecting STDOUT and STDERR from a cronjob to a file you can do this:
    0 0 1,2,3,4 * 1 perlscript.pl > program.log 2>&1


    --perlplexer
      Thanks, that's what we had before when I was running the second script directly but now I've got this wrapper and I wish the wrapper to handle logging.

      dpatrick
      - I think scsh is cool.
      Open Sourceror, Perlmonk
      http://perlmonk.org/~dpatrick

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2023-04-01 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (77 votes). Check out past polls.

    Notices?