keyongtech


  keyongtech > perl > 01/2004

 #1  
01-17-04, 01:02 AM
Larry Guest
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.
 #2  
01-17-04, 01:13 AM
Drieux
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

---
 #3  
01-17-04, 01:13 AM
Paul Johnson
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.
 #4  
01-17-04, 01:29 AM
John McKown
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.
 #5  
01-17-04, 05:55 PM
Randal L. Schwartz
>>>>> "Larry" == Larry Guest <larry> 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";
 #6  
01-18-04, 07:21 AM
Larry Guest
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 Replication is comple"..., 65Backup and Replication
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:merlyn]
Sent: Saturday, January 17, 2004 9:55 AM
To: beginners
Subject: Re: Redirect stdout, stderr to file and stdout


>>>>> "Larry" == Larry Guest <larry> 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";
 #7  
01-18-04, 07:38 AM
Larry Guest
I think I have it.

I had to have

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

-----Original Message-----
From: Larry Guest [mailto:larry]
Sent: Saturday, January 17, 2004 11:22 PM
To: 'Randal L. Schwartz'; beginners
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 Replication is comple"..., 65Backup and Replication
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:merlyn]
Sent: Saturday, January 17, 2004 9:55 AM
To: beginners
Subject: Re: Redirect stdout, stderr to file and stdout


>>>>> "Larry" == Larry Guest <larry> 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";
 #8  
01-20-04, 10:42 AM
Jenda Krynicky
From: "Larry Guest" <larry>
> 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 http://Jenda.Krynicky.cz

Jenda
===== Jenda === http://Jenda.Krynicky.cz =====
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
Similar Threads
Redirect STDOUT and STDERR to file in ksh

First up, apologies if this is a really dumb question ("There are no dumb questions, but there are a lot of inquisitive idiots")... I'm running a perl script which just...

How to redirect stdout, stderr in a GUI application

I would like to redirect stdout and stderr in a GUI application. My GUI is composed of a mainframe with a ClistCrl(COutPutWnd) control on the bottom. Inside my code I would...

bash : how to redirect both stdout and stderr to append a file

Hi, I can use "&>" to redirect both stdout and stderr to a file, then I wonder what is the counterpart of appending stdout and stderr to a file? I tried "&>>", but it does...

How to redirect STDOUT &STDERR?

How to redirect STDOUT &STDERR to screen and a logfile?

redirect stdout & stderr tomcat 4.1

I've reached total Google overload. Where, exactly do you set this?


All times are GMT. The time now is 04:10 AM. | Privacy Policy