Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by flexvault (Parson)
on Jan 14, 2013 at 18:01 UTC ( #1013252=note: print w/ replies, xml ) Need Help??


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

AM,

Two things after a glace at your code;

  • $hrmin is a string, and you're testing as a number.
    if ( $hrmin eq '1114' ) {
  • '1114' is before noon, '2359' is before midnight. You also should be concerned about seconds. A lot can happen in 60 seconds.

You may want to buffer the end of the day, which may give you a more accurate end-of-day and start-of-day.

Good Luck!

"Well done is better than well said." - Benjamin Franklin


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 0day (Sexton) on Jan 14, 2013 at 18:15 UTC
    I believe that this time it was put to test.
    However, if for one minute data are not received, this condition is not met. if($hrmin == 1114)
      update: MidLifeXis' explanation got thru my thick skull... and seems plausible. Apologies for wasting the effort of so many electrons and Monks but I do think the last graf still stands... and, FTR, a comment or narrative explanation of "1144" by the OP would have been, IMO, highly desireable.

      Monk 0day: Would you kindly explain your statements? I don't see that either has any relevance.

      "1144" remains a representation of an hour and minute that are not even close to midnight but it's being used as the code for the question "Is it almost midnite, and time to close the old log file and open a new one?"

      and NJBTW, any update of $ymd " after sleep 1 at 11:44AM is not going to be any different than the old one, so while there will be a new file created, it will have the same name as (and overwrite) the previous one. The test MUST occur much closer to midnight and with an appropriate sleep to ensure the calendar turns to another day.

        The 1114 was close to the posting time of the node. The assumption that I made is the same as 0day's, that the time provided was to allow for testing during the work day.

        It could should have been stated explicitly by the OP that '1114' was a time used for testing.

        The second statement is saying that unless data is received on the socket in the specified minute, the rollover code is never executed.

        --MidLifeXis

Re^2: Close a file and reopen a new one at midnight
by Anonymous Monk on Jan 14, 2013 at 19:12 UTC
    This is what I changed it to. The time is not 0000 for testing. It still does not work.
    #!/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"); while (my $line = <$sock>) { # Close existing $file and reopen a new one at time listed. if($hrmin eq 1228) { 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: )/) {

      • You now should have updated the $hrmin and $ymd variables.
      • You still have a problem if no data is received at '1228'.
      • You still have a problem overwriting a file if this gets restarted.
      • There are also options out there that handle this for you (one that is log4j-like, one that is tied, listed below by clueless_newbie, and probably others). See FileRotate for some options.

      --MidLifeXis

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-08-23 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (177 votes), past polls