一、思路
定時刪除日志,其實分為兩個過程:
- 查找符合條件的日志並刪除
- 定時
過程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
完工