伊格纳西奥的回答引起了我的兴趣,所以我做了一些研究,并提出了下面的Perl脚本.如果您的服务将写入命名管道,它应该工作并可用于logrotate.
要使它工作,您需要将日志文件设置为命名管道.然后重命名现有文件
mkfifo /var/log/something.log
并编辑3个文件名以满足您的要求.运行您的服务,然后运行此守护程序,该守护程序应读取命名管道并将其写入新的日志文件.
如果你重命名/var/log/somethingrotateable.log然后将一个HUP发送到守护进程,它将自己生成并创建一个新的somethingrotateable.log来写入.如果使用logrotate一个postrotate脚本kill -HUP’cat /var/run/yourpidfile.pid’
#!/usr/bin/perl -w
use POSIX ();
use FindBin ();
use File::Basename ();
use File::Spec::Functions;
#
$|=1;
#
# Change the 3 filenames and paths below to meet your requirements.
#
my $FiFoFile = '/var/log/something.log';
my $LogFile = '/var/log/somethingrotateable.log';
my $PidFile = '/var/run/yourpidfile.pid';
# # make the daemon cross-platform,so exec always calls the script
# # itself with the right path,no matter how the script was invoked.
my $script = File::Basename::basename($0);
my $SELF = catfile $FindBin::Bin,$script;
#
# # POSIX unmasks the sigprocmask properly
my $sigset = POSIX::SigSet->new();
my $action = POSIX::SigAction->new('sigHUP_handler',$sigset,&POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGHUP,$action);
sub sigHUP_handler {
# print "Got SIGHUP";
exec($SELF,@ARGV) or die "Couldn't restart: $!\n";
}
#open the logfile to write to
open(LOGFILE,">>$LogFile") or die "Can't open $LogFile";
open(PIDFILE,">$PidFile") or die "Can't open PID File $PidFile";
print PIDFILE "$$\n";
close PIDFILE;
readLog();
sub readLog {
sysopen(FIFO,$FiFoFile,0) or die "Can't open $FiFoFile";
while ( my $LogLine = ) {
print LOGFILE $LogLine;
}
}