前些日子,系统上线了,发现tomcat 下的日志增长得挺快的,写了个脚本清除n天以前的日志

 

  1. web_def_tomcat_log_path=/Application/tomcat/log  
  2. $web_def_tomcat_log_expire_days=7  
  3. if [ "$web_def_tomcat_log_expire_days" -gt 0 ]  
  4. then 
  5. echo "find $web_def_tomcat_log_path -follow -mtime +$web_def_tomcat_log_expire_days -name '*.log' -exec rm -f {} \;" 
  6. find $web_def_tomcat_log_path -follow -mtime +$web_def_tomcat_log_expire_days -name '*.log' -exec rm -f {} \;  
  7. fi 

由于web的mysql下面挂的同步太多了,每天产生1-2G的mysql-bin文件,因此也写了个清除mysql-bin的文件,这个文件会至少保留最新的一个mysql-bin.0*文件

 

  1. web_def_host="1.2.3.4" #mysql的ip  
  2. web_def_port=3306 #mysql的端口  
  3. web_def_username="tester" #mysql的用户  
  4. web_def_password="123" #mysql的密码  
  5. web_def_mysql_expire_logs_days=7 #删除7天以前的mysql-bin,但是最后至少保留一个文件  
  6. mysql_exe="mysql -h $web_def_host -P $web_def_port -u $web_def_username --password=$web_def_password -e " 
  7. if [ "$web_def_mysql_expire_logs_days" -gt 0 ]  
  8. then 
  9. #获取n天以前被修改的mysql-bin文件,  
  10. logFileName=`find $web_def_mysql_data_path -follow -atime -$web_def_mysql_expire_logs_days -name 'mysql-bin.0*' |sort|head -1`  
  11. if [ "$logFileName" != "" ]  
  12. then 
  13. logFileName=`basename $logFileName`  
  14. echo $mysql_exe "\"PURGE MASTER LOGS TO '$logFileName'\"" 
  15. $mysql_exe "PURGE MASTER LOGS TO '$logFileName'";  
  16. fi  
  17. fi 

上面这两个脚本,需要在crontab 里每天执行一次,定时删除n天以前的日志文件