Linux应用程序显示旋转,linux – 旋转愚蠢的非交互式应用程序的日志

伊格纳西奥的回答引起了我的兴趣,所以我做了一些研究,并提出了下面的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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值