in reply to Re^2: how to log a process
in thread how to log a process

Ok, here is a suggestion (not tested with data):
First, open the files and directories that you need so that this is all in one place at the beginning. I mean if this was a huge calculation monster, you wouldn't want it to run for an hour only to find out that you couldn't open the file to write the data to! That's a style thing for future reference.
File and directory handles will close when the program exits. This program will run in far less than one second and is just ~20 lines. Don't worry about close($x).
#!/usr/bin/perl -w use strict; my $logfile = "script_logs/30DyDlt-LOG.txt"; open (LOGFILE, ">>$logfile") or die "Cannot open logfile $logfile for +append $!"; my $dir = 'oven_web/iportal/cgi-bin/uploads'; opendir(DIR,$dir) || die "Can't open directory $dir $!"; my @files = grep{-f "$dir/$_"} readdir(DIR); my $current_date_time = time(); #this is seconds +- epoch value! my $thirty_days = 60 * 60 * 24 *30; #num seconds in 30 days foreach my $file(@files) { my $file_time = (stat("$dir/$file"))[9]; if ( $file_time < ($current_date_time - $thirty_days) ) { print "Deleting $dir/$file...\n"; #for the user on terminal unlink("$dir/$file") or die "unable to unlink file $file in $dir $ +!"; print LOGFILE "$dir/$file deleted\n"; } }
Update: could be better: have fun!
foreach $file (readdir D) { if ((-M "$dir/$file" > 30) && !unlink("$dir/$file")) { print "Oh no!!!!!\n"; } # else it worked ### }

Replies are listed 'Best First'.
Re^4: how to log a process
by zimbot (Initiate) on May 28, 2009 at 23:36 UTC
    Ok -- that didn't exctly work ---BUT SOLUTION! -- and certainly yallz wisdom proved Exactly what i needed: I think this is working - is logging & deleating
    ###:WAS Oven1 = '/media/oven_web/iportal/uploads' # rvsd to oven2 - 4-2009 js # /oven_web/iportal/uploads # # to del files older than 30 days ### 2.28.2008 - js #!/usr/local/bin/perl # 2592000 = 30 day n min (60 *60 * 24 *30 ) use strict; # change the below to dlt - my $dir = '/oven_web/iportal/cgi-bin/uploads2'; my $now2 = localtime time; my $logfile = "/oven_script/script_logs/30DayDlt-LOG.txt"; open (LOGFILE, ">>$logfile") or die "Cannot open logfile $logfile for +append $!"; opendir(DIR,$dir) || die "Can't open $dir : $!\n"; my @files = readdir(DIR); # you may want to grep only certain files he +re close(DIR); print LOGFILE "$now2 \n"; foreach my $file(@files) { my $now = time; my @stat = stat("$dir/$file"); if ($stat[9] < ($now - 2592000)) { print "Deleting $dir/$file..."; #unlink("$dir/$file"); #print "Done.\n"; print LOGFILE "$dir/$file deleted\n"; } }
    I know you had some ...more sophisicated ways of dealing with the time... I fell back on what I understood. ( grin ) I suppose I *could be premature with the happy dance -- but. It sure looks good. well, the odds look good. I am sure the code looks clumsy to you. How about that double var for current time $now & $now2 ( i am to happy to be ashamed -- refinement will come) all the thanks to you here is a question ? do i need to Close the log at the end?
      I would put a check on status of the unlink. But failure is like one in a million for something like this.

      It sounds like you are happy.
      I am happy about that!

        curious -- what do you mean by " I would put a check on status of the unlink. " ? thanks