当前位置 博文首页 > vic的博客:CentOS 6、CentOS 7 ,使用crontab和logrotate实现Ng

    vic的博客:CentOS 6、CentOS 7 ,使用crontab和logrotate实现Ng

    作者:[db:作者] 时间:2021-09-12 21:23

    CentOS 6、CentOS 7 ,使用crontab和logrotate实现Nginx日志轮询(转存)

    1、安装crontab
    2、安装logrotate日志文件管理工具
    3、分析cron.daily
    4、Nginx日志轮询(转存)配置,Apache的类似
    5、为什么日志轮询(转存)生成日志的时间是凌晨三四点
    6、立即执行logratate
    7、logratate常用配置参数

    1、安装crontab
    if ! which crond >/dev/null 2>&1;then yum install cronie -y; fi
    systemctl enable crond

    2、 安装logrotate日志文件管理工具
    yum -y install logrotate
    查看logrotate的软件包的信息
    rpm -ql logrotate
    /etc/cron.daily/logrotate
    /etc/logrotate.conf
    /etc/logrotate.d
    .....
    .....

    3、分析cron.daily
    cd /etc/cron.daily
    [root@www.zhangfangzhou.cn cron.daily]# ls
    logrotate makewhatis.cron
    [root@www.zhangfangzhou.cn cron.daily]# tail logrotate
    #!/bin/sh

    /usr/sbin/logrotate /etc/logrotate.conf
    EXITVALUE=$?
    if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
    fi
    exit 0

    4、Nginx日志轮询(转存)配置,Apache日志轮询(转存)类似。
    /home/wwwlogs/ 网站日志目录

    cat > /etc/logrotate.d/nginx << EOF
    /home/wwwlogs/*log {
    daily
    rotate 5
    missingok
    dateext
    compress
    notifempty
    sharedscripts
    postrotate
        [ -e /usr/local/nginx/logs/nginx.pid ] && kill -USR1 \`cat /usr/local/nginx/logs/nginx.pid\`
    endscript
    }
    EOF
    

    5、为什么日志轮询(转存)生成日志的时间是凌晨三四点?
    Logrotate是基于CRON运行的,所以这个时间是由CRON控制的,具体可以查询CRON的配置文件「/etc/crontab」
    anacron 正在运行过去 crontab 未进行的各项工作排程

    cat /etc/anacrontab
    # /etc/anacrontab: configuration file for anacron

    # See anacron(8) and anacrontab(5) for details.

    SHELL=/bin/sh
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    # the maximal random delay added to the base delay of the jobs
    RANDOM_DELAY=45 #最大延迟时间
    # the jobs will be started during the following hours only
    START_HOURS_RANGE=3-22 #只在03到22点之间执行

    #period in days delay in minutes job-identifier command
    1 5 cron.daily nice run-parts /etc/cron.daily
    7 25 cron.weekly nice run-parts /etc/cron.weekly
    @monthly 45 cron.monthly nice run-parts /etc/cron.monthly

    每天都执行/etc/cront.daily/目录下的脚本文件,
    真实的延迟RANDOM_DELAY+delay。这里的延迟是5分钟,加上上面的RANDOM_DELAY,所以实际的延迟时间是5-50之间,开始时间为03-22点,
    如果机器没关,那么一般就是在03:05-03:50之间执行。nice命令将该进程设置为nice=10,默认为0,即低优先级进程。
    如果RANDOM_DELAY=0,那么表示准确延迟5min,即03:05执行cron.daily

    6、立即执行logratate
    ldconfig #执行
    或者
    /etc/cron.daily
    logrotate
    或者
    执行下面
    /usr/sbin/logrotate -f /etc/logrotate.d/nginx

    由于logratate已经加到cron.daily(/etc/cron.daily/logrotate),不再需要加到计划任务中

    7、logratate常用配置参数
    /home/wwwlogs/*log:需要轮询日志路径
    daily:每天轮询
    rotate 5:保留最多5次滚动的日志
    missingok:如果日志丢失,不报错继续滚动下一个日志
    dateext:使用日期作为命名格式
    compress:通过gzip压缩转储以后的日志
    notifempty:当日志为空时不进行滚动
    /usr/local/nginx/logs/nginx.pid: nginx pid位置,请查看nginx.conf
    postrotate/endscript:在截断转储以后需要执行的命令

    CentOS 7 可能出现如下问题
    error: skipping "/home/wwwlogs/access_nginx.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
    解决方案
    vi /etc/logrotate.d/nginx
    su $http_user $http_group
    su www www

    转载至https://www.zhangfangzhou.cn/category/linux/centos/page/3#2

    cs