Help - Search - Member List - Calendar
Full Version: route STDOUT to file
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Dan
Hi
I've written a perl program which, when executed, fork()'s into the
background, doing it's job, so that the ssh session which executed it can be
closed.
The dilemma I have is the program crashes on random occasions due to an
unknown bug which I can't trace. The only method I would have of catching
the bug is to execute it in an ssh terminal and leave it open until it
crashes (which could be hours, days, or weeks). My connection's not the
stablest in the world, so this session may end up being prematurely
disconnected, and I'd still lose the output.
Is there a way or saying that every STDOUT, STDERR, die(), exit(), and any
error messages can be appended to a specified logfile, so I can maybe track
this bug? I've done a search on google for logging STDOUT and things, most
results of which give me modules which I don't really understand how it
would work.
Could anyone simply this for me, or give me any suggestions for things to
try/do/write to make this possible?

Many thanks in advance.

Dan

John W. Krahn
dan wrote:
QUOTE
Hi

Hello,

QUOTE
I've written a perl program which, when executed, fork()'s into the
background, doing it's job, so that the ssh session which executed it can be
closed.
The dilemma I have is the program crashes on random occasions due to an
unknown bug which I can't trace.
QUOTE
The only method I would have of catching
the bug is to execute it in an ssh terminal and leave it open until it
crashes (which could be hours, days, or weeks). My connection's not the
stablest in the world, so this session may end up being prematurely
disconnected, and I'd still lose the output.
Is there a way or saying that every STDOUT, STDERR, die(), exit(), and any
error messages can be appended to a specified logfile, so I can maybe track
this bug? I've done a search on google for logging STDOUT and things, most
results of which give me modules which I don't really understand how it
would work.
Could anyone simply this for me, or give me any suggestions for things to
try/do/write to make this possible?

You could try this (untested):

BEGIN {
my $log_file = ;
open STDOUT, '>', '/some/dir/logfile' or die "Cannot redirect STDOUT: $!";
open STDERR, ">&STDOUT" or die "Cannot dup STDOUT: $!";
}



John
--
use Perl;
program
fulfillment

Zentara
On Fri, 24 Jun 2005 18:28:08 +0100, [Email Removed] (Dan) wrote:


QUOTE
Is there a way or saying that every STDOUT, STDERR, die(), exit(), and any
error messages can be appended to a specified logfile, so I can maybe track
this bug? I've done a search on google for logging STDOUT and things, most
results of which give me modules which I don't really understand how it
would work.
Could anyone simply this for me, or give me any suggestions for things to
try/do/write to make this possible?

Many thanks in advance.

There are a whole bunch of different ways, here are a few ideas to
roll your own, but there is the whole set of Log modules on Cpan.

###########################################
#!/usr/bin/perl
use warnings;
use strict;

my $logfilename = "$0-log";
{
use IO::Tee;
open my $log, ">", $logfilename or die "error opening log: $!";

*STDOUT = (
IO::Tee->new( *STDOUT{IO}, $log )
or die "error making tee: $!"
)
}
print "This is both written to stdout and a logfilen";


#####################################
#!/usr/bin/perl

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

#possible problem the script hangs and wont exit the "tee" command.
#so at end of script

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


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

#!/usr/bin/perl
my $variable;

# First, save away STDERR
open SAVEERR, ">&STDERR";
close STDERR;
open STDERR, ">", $variable or die "What the hell?n";

# Now print something to STDERR, redirected to $variable
print STDERR "This is a test.n";

# Now close and restore STDERR to original condition.
close STDERR;
open STDERR, ">&SAVEERR";


# Now test to see if $variable actually received the text.
print "Now for the real test.n";
print $variable;
###########################################

#!/usr/bin/perl

use IO::Tee;

$tee = IO::Tee->new($handle1, $handle2);
print $tee "foo", "bar";

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

$tee = IO::Tee->new(*STDOUT, *OUTFILE);
print $tee "Your data here" if 1;

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

#roll your own
open my $fh, ">test.out" or die "Can't open outfile.";
print $_ "Test string.n" for *STDOUT, $fh;
close $fh;
##############################################




--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html

Jose Nyimi
QUOTE
-----Message d'origine-----
De : [Email Removed]
[mailto:[Email Removed]] De la part de zentara
Envoy : samedi 25 juin 2005 13:36
: [Email Removed]
Objet : Re: route STDOUT to file


There are a whole bunch of different ways, ...


Here yet another one ;)

#!/usr/bin/perl
use strict;
use warnings;
use IO::Handle;

{
local *STDOUT;
local *STDERR;
local *FILE;

open FILE, ">log.txt" || die "Cannot write to log file!";
open STDOUT, ">&FILE" || die "Cannot redirect STDOUT";
open STDERR, ">&FILE" || die "Cannot redirect STDERR";

STDOUT->autoflush(1);
STDERR->autoflush(1);

print STDOUT "An"; #to log.txt
print STDERR "Bn"; #to log.txt
}

#after above {}, STDOUT and STDERR
#go back their original condition
print STDOUT "Cn"; #on the console
print STDERR "Dn"; #on the console

R,
Jos.

John W. Krahn
John W. Krahn wrote:
QUOTE
dan wrote:

I've written a perl program which, when executed, fork()'s into the
background, doing it's job, so that the ssh session which executed it
can be
closed.
The dilemma I have is the program crashes on random occasions due to an
unknown bug which I can't trace.


Maybe it's a Heisenbug?  http://en.wikipedia.org/wiki/Heisenbug

The only method I would have of catching
the bug is to execute it in an ssh terminal and leave it open until it
crashes (which could be hours, days, or weeks). My connection's not the
stablest in the world, so this session may end up being prematurely
disconnected, and I'd still lose the output.
Is there a way or saying that every STDOUT, STDERR, die(), exit(), and
any
error messages can be appended to a specified logfile, so I can maybe
track
this bug? I've done a search on google for logging STDOUT and things,
most
results of which give me modules which I don't really understand how it
would work.
Could anyone simply this for me, or give me any suggestions for things to
try/do/write to make this possible?


You could try this (untested):

BEGIN {
my $log_file = ;
open STDOUT, '>', '/some/dir/logfile' or die "Cannot redirect
STDOUT: $!";
open STDERR, ">&STDOUT"              or die "Cannot dup STDOUT: $!";
}

Oops, I pushed the send button too quickly, that should be:

BEGIN {
open STDOUT, '>', '/some/dir/logfile' or die "Cannot redirect STDOUT: $!";
open STDERR, ">&STDOUT" or die "Cannot dup STDOUT: $!";
}



John
--
use Perl;
program
fulfillment

Manish Sapariya
I use screen as my tool to deal with unstable network connection.
Its command line version of vncviewer.

Screen dameon allows me to reconnect the previously opened screen,
if I by mistake detache or exit the current shell, or network forces
my connection down.

I assume a non-perlish solution will do.

regards,
Manish


On 06/24/2005 10:58 PM, Dan wrote:
QUOTE
Hi
I've written a perl program which, when executed, fork()'s into the
background, doing it's job, so that the ssh session which executed it can be
closed.
The dilemma I have is the program crashes on random occasions due to an
unknown bug which I can't trace. The only method I would have of catching
the bug is to execute it in an ssh terminal and leave it open until it
crashes (which could be hours, days, or weeks). My connection's not the
stablest in the world, so this session may end up being prematurely
disconnected, and I'd still lose the output.
Is there a way or saying that every STDOUT, STDERR, die(), exit(), and any
error messages can be appended to a specified logfile, so I can maybe track
this bug? I've done a search on google for logging STDOUT and things, most
results of which give me modules which I don't really understand how it
would work.
Could anyone simply this for me, or give me any suggestions for things to
try/do/write to make this possible?

Many thanks in advance.

Dan



Luke Bakken
Manish Sapariya wrote:
QUOTE
I use screen as my tool to deal with unstable network connection.
Its command line version of vncviewer.

Screen dameon allows me to reconnect the previously opened screen,
if I by mistake detache or exit the current shell, or network forces
my connection down.

Hear hear I was going to suggest the same. 'screen' is a great program!


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.