Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Close a file and reopen a new one at midnight

by clueless newbie (Friar)
on Jan 14, 2013 at 18:47 UTC ( #1013259=note: print w/ replies, xml ) Need Help??


in reply to Close a file and reopen a new one at midnight

Being clueless, I was wondering why there's no file tie module that performs this task ...

Here you go ... Tie::Handle::FileWriteRotate

use Tie::Handle::FileWriteRotate; tie *FH, 'Tie::Handle::FileWriteRotate', dir=>'/some/dir', prefix=>'myapp', print FH "Logging a line\n"; print FH "Logging another line\n";


Comment on Re: Close a file and reopen a new one at midnight
Download Code
Re^2: Close a file and reopen a new one at midnight
by Anonymous Monk on Jan 14, 2013 at 19:26 UTC
    Here is the program without email info.
    #!/usr/bin/perl -w use strict; use IO::Socket; use MIME::Lite; use Time::Out qw(timeout); $SIG{PIPE} = "IGNORE"; $| = 1; my $nb_secs = 10; my $buf = ""; my $file; my $sock = new IO::Socket::INET (PeerAddr => '192.168.173.9', PeerPort => 4002, Proto => 'tcp', Type => SOCK_STREAM, ); die "cannot open socket" unless ($sock); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); my $hrmin = sprintf ("%02d%02d",$hour,$min); my $ymd = sprintf("%04d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$mday,$ +hour,$min,$sec); my $filename = "/tmp/$ymd.txt"; print $filename, "\n"; open $file, ">$filename" || die("Couldn't open $file"); Email startup message my $msg = MIME::Lite->new( From => To => Subject => Type =>'TEXT', Data => 'Started.' , ); $msg->send(); while (my $line = <$sock>) { # Close existing $file and reopen a new one at midnight. if($hrmin eq 0000) { sleep 1; close $file; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time); my $hrmin = sprintf ("%02d%02d",$hour,$min); my $ymd = sprintf("%04d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$md +ay,$hour,$min,$sec); my $filename = "/tmp/$ymd.txt"; print $filename, "\n"; open $file, ">$filename" || die("Couldn't open $file"); } if ($line =~ m/^(ALARM: )/) { timeout $nb_secs => sub { my $count = 0; until ($count == 5 ) #Read the next xx lines { $buf .= $sock->getline(); $sock->flush(); $count++; } }; if ($@){ my $msg = MIME::Lite->new( From => To => Cc => Subject =>'ALARM message from house', Type =>'TEXT', Data =>$buf , ); $msg->send(); #print $line; #print $buf; $line = ""; $buf = ""; } } if ($line =~ m/^(INFO: )/) { timeout $nb_secs => sub { my $count = 0; until ($count == 5 ) #Read the next xx lines { $buf .= $sock->getline(); $sock->flush(); $count++; } }; if ($@){ my $msg = MIME::Lite->new( From => To => Cc => Subject =>'INFO message from house', Type =>'TEXT', Data =>$buf , ); $msg->send(); #print $line; #print $buf; $line = ""; $buf = ""; } } if ($line =~ m/^HISTORY: /) { timeout $nb_secs => sub { my $count = 0; until ($count == 40 ) #Read the next xx lines { $buf .= $sock->getline(); $count++; } }; if ($@){ my $msg = MIME::Lite->new( From => To => Subject =>'History Page', Type =>'TEXT', Data =>$buf , ); $msg->send(); #print $line; #print $buf; $line = ""; $buf = ""; } } if ($line =~ m/^ALARM-JONN: /) { timeout $nb_secs => sub { my $count = 0; until ($count == 40 ) #Read the next xx lines { $buf .= $sock->getline(); $count++; } }; if ($@){ my $msg = MIME::Lite->new( From => To => Subject => Type =>'TEXT', Data =>$buf , ); $msg->send(); #print $line; #print $buf; $line = ""; $buf = ""; } } $SIG{TERM} = sub { my $msg = MIME::Lite->new( From => To => Subject => Type =>'TEXT', Data => 'Terminated.' , ); $msg->send(); die "\nKilling program.\n"; close $sock or die "close: $!"; }; $SIG{INT} = sub { my $msg = MIME::Lite->new( From => To => Subject => Type =>'TEXT', Data => 'Terminated.' , ); $msg->send();# die "\nKilling program.\n"; close $sock or die "close: $!"; }; #Print current line to open $file. print $file $line; } sleep 10; # allow sometime to exit close $file;

      "compiled" but not tested!

      Set the date of last write to "000000" and close and append to a new file when the date changes ie date of last write != today. Thus if nothing happens for a week no logs are created for those dead days.

      #!/usr/bin/perl -w use strict; use IO::Socket; use MIME::Lite; my $nb_secs = 10; my $buf = ""; my $file; my $sock = new IO::Socket::INET( PeerAddr => '192.168.173.9', PeerPort => 4002, Proto => 'tcp', Type => SOCK_STREAM, ); die "cannot open socket" unless ($sock); my ($lastwrite,$today)="000000"; while ( my $line = <$sock> ) { if ( $lastwrite ne ($today=sprintf("%02d%02d%02d",(localtime())[5, +4,3]))) { # Close existing $file and reopen a new one at when the date c +hanges close $file; my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isd +st ) = localtime(time); my $ymd = sprintf( "%04d%02d%02d%02d%02d%02d", $year + 1900, $ +mon + 1, $mday, $hour, $min, $sec ); my $filename = "/tmp/$ymd.txt"; print $filename, "\n"; open $file, ">>","$filename" || die("Couldn't open $file"); $lastwrite=$today; } #Print current line to open $file. print $file $line; } close $file;
      Ok here is how I got it to work. I used a signal handler to close and reopen the file.
      while (1) { $SIG{USR1} = sub { close $file; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time); my $hrmin = sprintf ("%02d%02d",$hour,$min); my $ymd = sprintf("%04d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$md +ay,$hour,$min,$sec); my $filename = "/tmp/$ymd.txt"; print $filename, "\n"; open $file, ">$filename" || die("Couldn't open $file"); }; while (my $line = <$sock>) { if ($line =~ m/^(ALARM: )/) {
      Now I can just use the log rotate system to send a killall -USR1 to start a new file.
        While this works for the file. I am not getting anything written to the file.
        #!/usr/bin/perl -w use strict; use IO::Socket; use MIME::Lite; use Time::Out qw(timeout); $SIG{PIPE} = "IGNORE"; $| = 1; my $nb_secs = 10; my $buf = ""; my $file; my $sock = new IO::Socket::INET (PeerAddr => '192.168.173.9', PeerPort => 4002, Proto => 'tcp', Type => SOCK_STREAM, ); die "cannot open socket" unless ($sock); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); my $hrmin = sprintf ("%02d%02d",$hour,$min); my $ymd = sprintf("%04d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$mday,$ +hour,$min,$sec); my $filename = "/var/flexshare/shares/logs/$ymd.txt"; print $filename, "\n"; open $file, ">$filename" || die("Couldn't open $file"); while (1) { $SIG{USR1} = sub { close $file; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time); my $hrmin = sprintf ("%02d%02d",$hour,$min); my $ymd = sprintf("%04d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$md +ay,$hour,$min,$sec); my $filename = "/var/flexshare/shares/logs/$ymd.txt"; print $filename, "\n"; open $file, ">$filename" || die("Couldn't open $file"); }; while (my $line = <$sock>) { #Print current line to open $file. print $file $line; if ($line =~ m/^(ALARM: )/) { timeout $nb_secs => sub { my $count = 0; until ($count == 5 ) #Read the next xx lines { $buf .= $sock->getline(); $sock->flush(); $count++; } }; if ($@){ my $msg = MIME::Lite->new( ..... $SIG{INT} = sub { die "\nKilling program.\n"; close $sock or die "close: $!"; }; } } sleep 10; # allow sometime to exit close $file;
        So what did I mess up?????

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013259]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-08-23 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls