以下脚本在 CentOS 上执行时有效,但它不会根据大小轮换日志。每次执行此脚本时都会生成一个新日志。谁能告诉我如何使这个脚本根据大小工作?
#!/usr/bin/perl
use Logfile::Rotate;
my $logfile = new Logfile::Rotate(
File => '/var/log/remotehost/fakepath/Syslog.log',
Count => 100,
Gzip => '/usr/bin/gzip',
size => 1*1024*1024,
sub {
open my $PID, '<', '/usr/lib/systemd/system/rsyslog.service' or
die "Unable to open pid file:$!\n";
chomp(my $pid = <$PID>);
close $PID;
kill 'HUP', $pid;
}
);
# Log file locked (really) and loaded. Now let's rotate it.
$logfile->rotate();
# make sure the log file is unlocked (destroying object unlocks file)
undef $logfile;
答案1
事实上,Logfile::Rotate
它不会根据大小进行旋转,这应该不足为奇,因为它的文档中没有提到它会这样做。也许根据大小进行旋转的最简单方法是将调用包装rotate
在 if 中,例如:
if (-s '/var/log/remotehost/fakepath/Syslog.log' > 1048576) {
$log->rotate();
}
仅当指定文件大于 1MB(大小以字节为单位)时,才会轮换日志。