Linux定时删除日志脚本

一、思路

定时删除日志,其实分为两个过程:

  1. 查找符合条件的日志并删除
  2. 定时
    过程1需要写一个查找脚本,过程2需要用到linux的crontab

二、查找并删除

# vim delete-logs.sh

删除指定目录下7天以上的日志文件:

find /home/trs/trswcmv7/Tomcat/logs -name "host-manager.*" -mtime +7 -exec rm -rf {} ;
find /home/trs/trswcmv7/Tomcat/logs -name "localhost.*" -mtime +7 -exec rm -rf {} ;
find /home/trs/trswcmv7/Tomcat/logs -name "manager.*" -mtime +7 -exec rm -rf {} ;
find /home/trs/trswcmv7/Tomcat/logs -name "catalina.*" -mtime +7 -exec rm -rf {} ;
find /home/trs/trswcmv7/Tomcat/logs -name "localhost_access_log*" -mtime +7 -exec rm -rf {} ;

find /home/data/bak/wcm -type d -mtime +120 -exec rm -rf {} ;

# chmod +x delete-logs.sh

三、linux定时任务

# crontab -e

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .-------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
  1 0 * * * /usr/sbin/ntpdate 10.11.4.1 &
 55 23 * * * /home/trs/delete-outofdate-logs.sh

完工

原文地址:https://www.cnblogs.com/greycdoer0/p/11072324.html