Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Anonymous Monk
on Jan 14, 2013 at 19:12 UTC ( #1013261=note: print w/ replies, xml ) Need Help??


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

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: )/) {


Comment on Re^2: Close a file and reopen a new one at midnight
Download Code
Re^3: Close a file and reopen a new one at midnight
by MidLifeXis (Monsignor) on Jan 14, 2013 at 19:18 UTC

    • 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://1013261]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2015-07-03 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (51 votes), past polls