Created
February 4, 2010 16:48
-
-
Save jelder/294849 to your computer and use it in GitHub Desktop.
Delete inactive Tomcat log files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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