下面的脚本在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;
发布于 2018-02-01 03:24:56
事实上,Logfile::Rotate
并不是根据大小旋转的,这应该是不足为奇的,因为它的文档中没有任何地方表明它会这样做。基于大小旋转的最简单方法可能是将对rotate
的调用封装在if中,例如:
if (-s '/var/log/remotehost/fakepath/Syslog.log' > 1048576) {
$log->rotate();
}
这应该只在指定的文件大于1MB (大小以字节为单位)时旋转日志。
https://unix.stackexchange.com/questions/421120
复制相似问题