> /var/spo...">
美文网首页
centos 定时清理log

centos 定时清理log

作者: 小草_fdba | 来源:发表于2019-05-07 10:40 被阅读0次

添加定时任务

echo "0 0 * * * sh /****/clear_log.sh" >> /var/spool/cron/root

clear_log.sh

#!/bin/bash

cd /var/log/cloudsec/
mv webServer.log webServer`date -d yesterday +%Y%m%d`.log;
filenames=$(ls)
prefix=webServer
dateLen=8
prefixLen=${#prefix}
for filename in ${filenames[@]}
do
    file_date=${filename:$prefixLen:$dateLen}
    regFileDate=`echo ${file_date} | grep '^[0-9][0-9]*$'`
    if [ ${filename:18:4} ] && [ ${filename:18:4} != '.log' ] && [ "$regFileDate" = "$file_date" ] && [ `date -d ${file_date} +%s` -lt `date -d -180day +%s` ];
    then
        echo $file_date
        rm -f $filename
    fi
done

这样写会发现log没有继续在webServer.log里写,而是在webServer.date.log里写,原因查看文件描述符

换用logrotate切割log
脚本如下

cd /etc/logrotate.d
touch web_log
echo "/**.log
{
  rotate 90
    daily
    dateyesterday
    copytruncate
}" >> /etc/logrotate.d/web_log

echo "# /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=0
# the jobs will be started during the following hours only
START_HOURS_RANGE=0-22

#period in days   delay in minutes   job-identifier   command
1       0       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/anacrontab

echo "0 0 * * * logrotate -vf /etc/logrotate.d/web_log" >> /var/spool/cron/root

相关文章

  • centos 定时清理log

    添加定时任务 echo "0 0 * * * sh /****/clear_log.sh" >> /var/spo...

  • 本人的wamp注意事项

    1.我的wamp要定时清理log我安装的wamp注意要定时清理一下loglog文件在wamp下面的logs文件夹里...

  • 清理linux日志 持续更新

    常见的日志文件如下: nginx log日志 java的进程的输出 /var/log/journal/ 清理未清理...

  • 定时清理

    我们的一生会经历很多很多的东西,全部都背着那会特别特别累。所以,定时清理回忆,定时清理不再需要的、或者很舍不得却在...

  • 定时清理

    日常生活中,定时清理,是更好的开始。有时候,我们只关注了买东西的瞬间,但却不注意定时清理。像不用的东西过了期,占用...

  • shell 脚本

    shell 清理log , #/bin/bash find /var/log/hadoop/ocdp/ -mtim...

  • centos7(4)之日志定时清理

    本文主要介绍的是Linux使用定时任务每周定时清理45天以前日志。服务器每天会产生很大的日志文件,为了不使硬盘被日...

  • Linux使用总结

    ubuntu定时任务日志文件位置一般linux系统的定时任务的日志文件都在/var/log/cron.log,今天...

  • day18

    如何调试定时任务 1.查看定时任务 /var/log/cron 学会最小化排除: 先清空,>/var/log/cr...

  • 断舍离

    人要学会定时清理,生活才会轻松。感情也是,定时清理感情的垃圾,人才不会被情困。 ————题...

网友评论

      本文标题:centos 定时清理log

      本文链接:https://www.haomeiwen.com/subject/qhasoqtx.html