centos7定時任務刪除tomcat日志


1、創建並編寫shell腳本文件clear_tomcat_logs.sh,內容如下

#!/bin/sh
find /usr/local/zzxsoft/schedule/logs/ -mtime +2 -type f -name zzx.* -exec rm -f '{}' \;
find /usr/local/zzxsoft/apache-tomcat-service/logs/ -mtime +2 -type f -name zzx.* -exec rm -f '{}' \;
find /usr/local/zzxsoft/apache-tomcat-web/logs/ -mtime +2 -type f -name zzx.* -exec rm -f '{}' \;

保存文件 然后該權限:  chmod 755 clear_tomcat_log.sh

以下對上面find語句的解釋:(以第一條shell語句為例)

  /usr/local/zzxsoft/schedule/logs/  --設置查找的目錄;
  -mtime +2 --設置時間為2天前;
  -type f --設置查找的類型為文件;
  -name *.sh[ab] --設置文件名稱中包含sha或者shb;
  -exec rm -f --查找完畢后執行刪除操作;
  '{}'  為操作的內容,此為固定格式;
  \    為轉義符;'{}' \中間有個空格,此處不加空格會報錯;
  ;   為shell語句結束符,表示與其他語句沒有關系,單獨執行;
 
PS:我踩過的坑:花了很長時間才解決(如果在系統內vi編輯不會出現此問題)
場景:為了省事我在我的主機Windows上先編輯好的文件用ssh工具上傳到線上服務器上,一直報錯: find: missing argument to `-exec  和  -bash: ./xxx.sh: /bin/sh^M 
原因:Windows的編輯文件的格式和linux是不一樣的
解決方案:通過notepad++編輯后選擇 Edit –> EOL Conversion –> UNIX/OSX Format  保存后再上傳即可
 
2、設置定時任務
   crontab -e  打開編輯定時任務,添加如下內容  
HRLL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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

# 每天凌晨6點自動清理tomcat日志信息
00 6 * * * /usr/local/zzxsoft/clear_tomcat_logs.sh

保存后重啟定時任務 service crond restart

如果沒有安裝crond.service會報錯:crontab: unrecognized service  需要yum安裝一下:yum -y install vixie-cron  即可結局

才疏學淺,如有錯誤,歡迎指正,謝謝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM