Skip to content

Instantly share code, notes, and snippets.

@jelder
Created February 4, 2010 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jelder/294849 to your computer and use it in GitHub Desktop.
Save jelder/294849 to your computer and use it in GitHub Desktop.
Delete inactive Tomcat log files.
#!/usr/bin/perl
# Delete inactive log files.
use strict;
use warnings;
my $dir = '/opt/tomcat6/logs';
# Files which are open
my %open;
open LSOF, "lsof -Fn $dir/* |";
$open{$_} = 1 for map { m/n(\/.*)/g } <LSOF>;
close LSOF;
# Files which have not been modified in an hour
my $inactive = time - (60 * 60);
my $bytes = 0;
for my $file (<$dir/*>) {
next if (stat($file))[9] >= $inactive;
next if $open{$file};
$bytes += (stat($file))[7];
unlink $file;
}
print "Freed $bytes bytes.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment