定时切割nginx的access.log

1 先写好切割日志的执行脚本!
实例代码好下:

#!/usr/bin/perl
use strict;
use POSIX qw/strftime/;
use File::Copy;

our ($hour,$min) = split/ /,strftime "%H %M",localtime;

rotate_log("pay.access.log");
#rotate_log("webservice.log");

sub rotate_log {
    my $log = shift;
    my $srcdir = "/data2/log/nginx";
    my $destdir = "/data2/nginx_log_backup";

    if ($hour == 0 and $min == 0) {  # run only at 00:00 each day
        my $lastday = strftime "%Y%m%d",localtime(time-3600);
        copy "$srcdir/$log","$destdir/$log.$lastday";
        system "echo -n > $srcdir/$log";
        system "gzip $destdir/$log.$lastday";
    }
} 
 


2 在liunx下面的/ect/crontab 下面建立一个定时任务.
比如:30 * * * * root /data/....(存放脚本的路径)

 

你可能感兴趣的:(nginx,webservice,脚本,perl,Access)