Professional Web Applications Themes

Redirect stdout, stderr to file and stdout - PERL Beginners

I have a small script that does some admin work for me. What I need to do now is not only have is display information to STDERR and STDOUT but also write the same information I see when I run the command to a file. I have written it for others who are not very technical. I want them to be able to see the script working and what it is doing. But I also need a log file of the same information so I can have it mailed to me and keep a copy on the server. I cant ...

  1. #1

    Default Redirect stdout, stderr to file and stdout

    I have a small script that does some admin work for me.

    What I need to do now is not only have is display information to STDERR
    and STDOUT but also write the same information I see when I run the
    command to a file.

    I have written it for others who are not very technical. I want them to
    be able to see the script working and what it is doing. But I also need
    a log file of the same information so I can have it mailed to me and
    keep a copy on the server.

    I cant seem to get this to work.

    Larry Guest Guest

  2. #2

    Default Re: Redirect stdout, stderr to file and stdout


    On Jan 16, 2004, at 5:02 PM, Larry Guest wrote:
    > I have a small script that does some admin work for me.
    >
    > What I need to do now is not only have is display information to STDERR
    > and STDOUT but also write the same information I see when I run the
    > command to a file.
    >
    > I have written it for others who are not very technical. I want them
    > to
    > be able to see the script working and what it is doing. But I also
    > need
    > a log file of the same information so I can have it mailed to me and
    > keep a copy on the server.
    Here's a bit of a wacky Idea - solve it by
    a bit of indirection - namely write the
    code so that it does what you want it to do
    then nest it inside of a script like say

    #!/bin/sh

    myPerlCode 2>&1 | tee /tmp/myOutput.$$

    mail me -s "command run" < /tmp/myOutput.$$
    /bin/rm -f /tmp/myOutput.$$

    that way your perl code remains simple, but what you
    hand out to the users to use is going to deal with
    all of the STDOUT|STDERR as well a always sending
    you the email...


    ciao
    drieux

    ---

    Drieux Guest

  3. #3

    Default Re: Redirect stdout, stderr to file and stdout

    On Fri, Jan 16, 2004 at 05:02:52PM -0800, Larry Guest wrote:
    > I have a small script that does some admin work for me.
    >
    > What I need to do now is not only have is display information to STDERR
    > and STDOUT but also write the same information I see when I run the
    > command to a file.
    >
    > I have written it for others who are not very technical. I want them to
    > be able to see the script working and what it is doing. But I also need
    > a log file of the same information so I can have it mailed to me and
    > keep a copy on the server.
    >
    > I cant seem to get this to work.
    You don't say what OS you are on, but on *nix I would do something like:

    $ perl -le 'print STDERR "stderr"; print "stdout"' 2>&1 | tee output.txt

    Solutions in Perl take more effort, but we can discuss them if necessary.

    --
    Paul Johnson - [email]paulpjcj.net[/email]
    [url]http://www.pjcj.net[/url]
    Paul Johnson Guest

  4. #4

    Default RE: Redirect stdout, stderr to file and stdout


    One quick and dirty solution that I've used for some debugging in the
    past is to create a subroutine called PrintOut();


    ###############################

    use strict;
    use warnings;

    open(OUTFILE,">mylog.txt") || die;
    PrintOut("Hello World\n");

    sub PrintOut{
    print _;
    print OUTFILE _;
    }

    ###############################

    -----Original Message-----
    From: Larry Guest [mailto:larryopsource.net]
    Sent: Friday, January 16, 2004 5:03 PM
    To: [email]beginnersperl.org[/email]
    Subject: Redirect stdout, stderr to file and stdout


    What I need to do now is not only have is display information to STDERR
    and STDOUT but also write the same information I see when I run the
    command to a file.

    Tim Johnson Guest

  5. #5

    Default Re: Redirect stdout, stderr to file and stdout

    On Fri, 16 Jan 2004, Larry Guest wrote:
    > I have a small script that does some admin work for me.
    >
    > What I need to do now is not only have is display information to STDERR
    > and STDOUT but also write the same information I see when I run the
    > command to a file.
    >
    > I have written it for others who are not very technical. I want them to
    > be able to see the script working and what it is doing. But I also need
    > a log file of the same information so I can have it mailed to me and
    > keep a copy on the server.
    >
    > I cant seem to get this to work.
    >
    What have you tried? I would alter the script to have multiple print
    statements. One to STDERR or STDOUT, the other to my file. Something like:

    open(LOGGER,'myfile.log');
    ....
    print STDERR "$message\n";
    print LOGGER "$message\n";
    ....

    If you want, you could "encapsulate" this by not using print directly, but
    in your own function, say "myprint()".

    sub myprint {
    my $fn = shift;
    print $fn "_";
    print LOGGER "_";
    }

    my $STDOUT=\STDOUT;
    my $STDERR=\STDERR;
    ....
    my $message="This is a test.\n";
    ....
    &myprint($STDOUT,$message);
    ....
    &myprint($STDERR,$message);

    Hope this is useful in some way.

    --
    Maranatha!
    John McKown

    John McKown Guest

  6. #6

    Default Re: Redirect stdout, stderr to file and stdout

    >>>>> "Larry" == Larry Guest <larryopsource.net> writes:

    Larry> I have a small script that does some admin work for me.
    Larry> What I need to do now is not only have is display information to STDERR
    Larry> and STDOUT but also write the same information I see when I run the
    Larry> command to a file.

    Larry> I have written it for others who are not very technical. I want them to
    Larry> be able to see the script working and what it is doing. But I also need
    Larry> a log file of the same information so I can have it mailed to me and
    Larry> keep a copy on the server.

    Larry> I cant seem to get this to work.

    I think this might do it:

    open TEE, "| tee YourLogFileHere";
    open STDOUT, ">&TEE";
    open STDERR, ">&TEE";

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlynstonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal L. Schwartz Guest

  7. #7

    Default RE: Redirect stdout, stderr to file and stdout

    This is very close.

    But the script hangs and wont exit the "tee" command.

    I ran the script using "strace" and it sits there like this.

    write(1, "Backup and tion is comple"..., 65Backup and tion
    is complete for mysite.com
    ) = 65
    close(3) = 0
    munmap(0x401fd000, 4096) = 0
    rt_sigaction(SIGHUP, {SIG_IGN}, {SIG_DFL}, 8) = 0
    rt_sigaction(SIGINT, {SIG_IGN}, {SIG_DFL}, 8) = 0
    rt_sigaction(SIGQUIT, {SIG_IGN}, {SIG_DFL}, 8) = 0
    wait4(3576,



    And a ps-ef looks like this.

    root 3576 3575 0 02:19 pts/0 00:00:00 tee YourLogFileHere

    I have a close (TEE); at the end as well.

    Any thoughts?

    Thanks

    -----Original Message-----
    From: Randal L. Schwartz [mailto:merlynstonehenge.com]
    Sent: Saturday, January 17, 2004 9:55 AM
    To: [email]beginnersperl.org[/email]
    Subject: Re: Redirect stdout, stderr to file and stdout

    >>>>> "Larry" == Larry Guest <larryopsource.net> writes:
    Larry> I have a small script that does some admin work for me. What I
    Larry> need to do now is not only have is display information to STDERR
    Larry> and STDOUT but also write the same information I see when I run
    Larry> the command to a file.

    Larry> I have written it for others who are not very technical. I want
    Larry> them to be able to see the script working and what it is doing.
    Larry> But I also need a log file of the same information so I can have
    Larry> it mailed to me and keep a copy on the server.

    Larry> I cant seem to get this to work.

    I think this might do it:

    open TEE, "| tee YourLogFileHere";
    open STDOUT, ">&TEE";
    open STDERR, ">&TEE";

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777
    0095 <merlynstonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See
    PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
    training!

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email]
    For additional commands, e-mail: [email]beginners-helpperl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>



    Larry Guest Guest

  8. #8

    Default RE: Redirect stdout, stderr to file and stdout

    I think I have it.

    I had to have

    close (STDERR)
    close (STDOUT)
    close (TEE)

    -----Original Message-----
    From: Larry Guest [mailto:larryopsource.net]
    Sent: Saturday, January 17, 2004 11:22 PM
    To: 'Randal L. Schwartz'; [email]beginnersperl.org[/email]
    Subject: RE: Redirect stdout, stderr to file and stdout


    This is very close.

    But the script hangs and wont exit the "tee" command.

    I ran the script using "strace" and it sits there like this.

    write(1, "Backup and tion is comple"..., 65Backup and tion
    is complete for mysite.com
    ) = 65
    close(3) = 0
    munmap(0x401fd000, 4096) = 0
    rt_sigaction(SIGHUP, {SIG_IGN}, {SIG_DFL}, 8) = 0 rt_sigaction(SIGINT,
    {SIG_IGN}, {SIG_DFL}, 8) = 0 rt_sigaction(SIGQUIT, {SIG_IGN}, {SIG_DFL},
    8) = 0 wait4(3576,



    And a ps-ef looks like this.

    root 3576 3575 0 02:19 pts/0 00:00:00 tee YourLogFileHere

    I have a close (TEE); at the end as well.

    Any thoughts?

    Thanks

    -----Original Message-----
    From: Randal L. Schwartz [mailto:merlynstonehenge.com]
    Sent: Saturday, January 17, 2004 9:55 AM
    To: [email]beginnersperl.org[/email]
    Subject: Re: Redirect stdout, stderr to file and stdout

    >>>>> "Larry" == Larry Guest <larryopsource.net> writes:
    Larry> I have a small script that does some admin work for me. What I
    Larry> need to do now is not only have is display information to STDERR
    Larry> and STDOUT but also write the same information I see when I run
    Larry> the command to a file.

    Larry> I have written it for others who are not very technical. I want
    Larry> them to be able to see the script working and what it is doing.
    Larry> But I also need a log file of the same information so I can have
    Larry> it mailed to me and keep a copy on the server.

    Larry> I cant seem to get this to work.

    I think this might do it:

    open TEE, "| tee YourLogFileHere";
    open STDOUT, ">&TEE";
    open STDERR, ">&TEE";

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777
    0095 <merlynstonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See
    PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
    training!

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email]
    For additional commands, e-mail: [email]beginners-helpperl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>




    --
    To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email]
    For additional commands, e-mail: [email]beginners-helpperl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>



    Larry Guest Guest

  9. #9

    Default Re: Redirect stdout, stderr to file and stdout

    From: "Larry Guest" <larryopsource.net>
    > What I need to do now is not only have is display information to
    > STDERR and STDOUT but also write the same information I see when I run
    > the command to a file.
    Strange you only got a few Unix-only solutions when there are at
    least two modules that do this on any OS at all.

    See IO::Tee on CPAN or Local::TeeOutput on [url]http://Jenda.Krynicky.cz[/url]

    Jenda
    ===== [email]JendaKrynicky.cz[/email] === [url]http://Jenda.Krynicky.cz[/url] =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Krynicky Guest

Similar Threads

  1. Run one process and get the stdout and stderr
    By Marcos Rebelo in forum PERL Beginners
    Replies: 2
    Last Post: October 16th, 12:01 PM
  2. [PHP-DEV] STDOUT, STDERR not defined in CLI mode
    By Alex Kiesel in forum PHP Development
    Replies: 0
    Last Post: August 8th, 11:36 AM
  3. 1.8.0, fcgi and $stdout/$stderr
    By Brian Candler in forum Ruby
    Replies: 1
    Last Post: July 28th, 10:11 AM
  4. redirecting stdout/stderr on execl
    By shea martin in forum UNIX Programming
    Replies: 3
    Last Post: July 21st, 11:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139