#!/usr/local/bin/perl5 # Name: backyard/hourly.pl Author: js-cgi@inwap.com 08-Sep-2001 # Purpose: Moves webcam pictures to other directories, once an hour. # Deletes old snaps after 48 hours. # # This can be used on any UNIX system (including those running the Apache # web server) by adding this line to your crontab file: # 0 * * * * backyard/hourly.pl -q # # It can also be run on a PC by using the System Agent (which is on the # 'Windows 95 Plus! Pack' and built-in to Windows 98). # Cmd Line: c:\perl\bin\wperl.exe e:/www/backyard/hourly.pl # Descripton: Delete old SnapCAP images # Start In: e:\www\backyard # Run: minimized # Schedule: Hourly, 3 minutes past the hour # # Most recent patch: Fix S1G bug that occurred "Sat Sep 8 18:46:39 2001 PST". unless (($this_dir,$this_prog) = $0 =~ m%(.*)/(.*)%) { ($this_dir,$this_prog) = (".",$0); } chdir("$this_dir") || die "$0: Could not cd into $this_dir: $!\n"; $verbose = 1; # Default is medium verbosity $verbose = 2 if $ARGV[0] eq "-v"; # Use "-v" for very verbose $verbose = 0 if $ARGV[0] eq "-q"; # Quiet mode from cron print "\$0 = $0, this_dir = $this_dir\n" if $verbose > 1; $newest_time = 0; # To get newest file in current-hour # These calls to the &move routine do all the work. It goes to the # specified subdirectory, and any file older than a certain number of # hours is either deleted or moved to a different subdirectory. # # The first argument is the name of the subdirectory to look at. # The second argument is how many hours to keep files in the subdirectory. # If a third argument is present, old files will be renamed to the # specified directory instead of being deleted. &move("current-hour", 2); # Delete images older than 2 hours &move("current-today", 24,"current-yester"); # Move images older than 1 day &move("current-yester",48); # Delete images older than 48 hours &move(".video/1",1); # Delete raw captures older than 1 hour &move(".video/2",1); # Delete raw captures older than 1 hour &move(".video/3",1); # Delete raw captures older than 1 hour exit 0; sub move { local($dir,$hours,$move_to) = @_; opendir(DIR,$dir); @imgs = grep(/jpg$|gif$/i,readdir(DIR)); closedir(DIR); @imgs = grep(!/capture/,@imgs); # Skip capture_t.jpg if ($verbose > 1) { @gifs = grep(/gif/,@imgs); @jpgs = grep(/jpg/,@imgs); print scalar(@jpgs)," JPGs and ", scalar(@gifs)," GIFs $dir\n"; } return if scalar(@imgs) < 14; # Skip almost empty directories # Get the modification time of each file. Sort by time, oldest first. @files = (); foreach $_ (@imgs) { $mtime = (lstat("$dir/$_"))[9]; # This is 999_999_999 on 8-Sep-2001 push(@files,sprintf("%010d %s",$mtime,$_)) unless -l _;# Skip symlinks } @imgs = sort @files; # Put into chronological order $_ = pop(@imgs); # Never delete newest file print "Newest file: $_\n" if $verbose > 1; $_ = pop(@imgs); # Never delete newest file's thumbnail print "Newest file: $_\n" if $verbose > 1; ($mtime,$file) = split; $newest_time = $mtime if $mtime > $newest_time; foreach $_ (@imgs) { ($mtime,$file) = split; $age = ($newest_time - $mtime) / (60 * 60); printf "%2d %5.2f %s\n",$hours,$age,$file if $verbose > 1; if ($age > $hours) { if ($move_to) { if (rename("$dir/$file","$move_to/$file")) { print("move $dir/$file $move_to\n") if $verbose; } else { warn("Could not rename $dir/$file to $move_to: $!\n"); } } else { if (unlink("$dir/$file")) { print("delete $dir/$file\n") if $verbose; } else { warn("Could not delete $dir/$file: $!\n"); } } # end if($move_to) } # end if($age) } # end foreach } # end move{}