Skip to content

Instantly share code, notes, and snippets.

@dannysauer
Last active April 18, 2020 18:02
Show Gist options
  • Save dannysauer/de3b1f71aaf0d6cdfc466bb2d08526d1 to your computer and use it in GitHub Desktop.
Save dannysauer/de3b1f71aaf0d6cdfc466bb2d08526d1 to your computer and use it in GitHub Desktop.
perl script to drop into /etc/cron.hourly (or whatever) to clean up core files
#!/usr/bin/perl
# drop into /etc/cron.hourly (or whatever) to clean up core files
use warnings;
use strict;
use File::Find;
use File::LibMagic;
use Number::Bytes::Human;
sub wanted;
# global vars!
use vars qw/*name *prune $magic $human/;
*name = *File::Find::name;
#*dir = *File::Find::dir;
*prune = *File::Find::prune;
$magic = File::LibMagic->new();
$human = Number::Bytes::Human->new(bs=>1024, suffixes=>'si_1000');
File::Find::find(\&wanted, q{/});
exit;
sub wanted {
if ( $name eq q{/proc}
or $name eq q{/var/named/run-root/proc}
or $name eq q{/dev}
or $name eq q{/sys}
){
$prune = 1;
return;
}
return unless -f _;
return unless /^core\.\d+\z/s;
my $type = $magic->checktype_filename($name);
if( $type =~ m{application/x-(empty|coredump)} ){
my ($size,$atime) = (lstat(_))[7,8];
my $t = localtime($atime);
my $s = $size ? $human->format($size) : q{empty};
print(qq{removing $s file '$name' from $t ($type)\n});
unlink($name) or warn qq{Failed removing $name\n};
}
}
@dannysauer
Copy link
Author

Ideally, things would be fixed so core files aren't generated all the time to begin with, but for stopgap purposes in a specific, annoying situation... 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment