【需求】
定时清理服务器的log,可以根据需要给不同的文件,设置定时清理时间
【脚本】
base_path="/data/app/log/" save_15_day_files=(A B) save_15_day_time=`date -d"-15 day" "+%Y-%m-%d"` for data in ${save_15_day_files[@]} do del_file_path=$base_path${data}'-'$save_15_day_time'.log' if [ -f "$del_file_path" ];then echo 'success, delete file is '$del_file_path rm -rf $del_file_path else echo 'error, no such file '$del_file_path fi done save_7_day_files=(A B) save_7_day_time=`date -d"-7 day" "+%Y-%m-%d"` for data in ${save_7_day_files[@]} do del_file_path=$base_path${data}'-'$save_7_day_time'.log' if [ -f "$del_file_path" ];then echo 'success, delete file is '$del_file_path rm -rf $del_file_path else echo 'error, no such file '$del_file_path fi done
【调用】
#写一个crontab的定时任务来执行 crontab -e #定时每天晚上九点执行 0 21 * * * /bin/sh /data/app/tools/clearLog.sh >> /data/app/log/shellDeleteLog.log 2>&1