日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

日志文件清理代码

發布時間:2025/5/22 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日志文件清理代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對于產生大量日志的服務器,要對日志進行控制,如果任由日志膨脹可能會寫滿磁盤造成服務器宕機。

要提的是代碼中幾個值得注意的地方:

命令說明
date_now=`date +"%Y-%m-%d"` 通過``符號將命令執行結果賦給變量
if ! [ -e ${clean_log_path} ] 中括號里外與其他內容都要有空格
delete_log_file(){ 函數定義{符號一定要緊接不能寫到下一行
${delete_log_prefix}${date_yes} 字符串連接直接寫不用任何連接符
${array_path[*]} 直接寫$array_path[*]會識別為$array_path不能成功遍歷數組;凡變量鼓勵都用定界符{}
delete_log_file 函數的定義要在使用之前,不然會提示找不到函數

?

?

?

?

?

?

?

?

?

?

?另外要注意Windows編緝的shell腳本會因回車換行問題致使執行時一直報錯,參看“Windows與Linux的回車換行轉換”

?

#code by ls in 20170427 date_now=`date +"%Y-%m-%d"` date_yes=`date -d "1 day ago" +"%Y-%m-%d"` #the drectory of this script,need to change for yourself clean_log_path="/iom_jk/IOM_INF/tomcat_6.0.44_1/clean_log/" if ! [ -e ${clean_log_path} ] thenmkdir -p $clean_log_path fi clean_log_file="${clean_log_path}clean_log.log" #the logfile of this script,need to change for yourself delete_log_prefix="log.log." #the prefix of delete file,need to change for yourself zero_log_postfix=".log" #the postfix of zero file,neet to change for yourself #those drectory that need to clean,neet to change for yourself array_path[0]="/iom_jk/IOM_INF/tomcat_6.0.44_1/webapps/IomInterface/WEB-INF/sh/logs" array_path[1]="/iom_jk/IOM_INF/tomcat_6.0.44_1/bin/logs" array_path[2]="/iom_jk/app/interface/IomInterface/WEB-INF/sh/logs"delete_log_file(){ #the function to delete yesterday log file,do not need to change in generalif [ -e ${delete_log_prefix}${date_yes} ]thenrm -f ${delete_log_prefix}${date_yes} && echo "${data_now} : ${delete_log_prefix}${date_yes} have been delete" >> ${clean_log_file}fi }zero_log_file(){ #the function to zero log file,do not need to change in generalfor file_name in `ls *${zero_log_postfix}`doecho "" > ${file_name} && echo "${data_now} : ${file_name} have been clean" >> ${clean_log_file}done }for path_log in ${array_path[*]} docd $path_logecho "$date_now : enter ${path_log}" >> ${clean_log_file}delete_log_file #the function to delete yesterday log file,need to change for yourselfzero_log_file #the function to zero log file,need to change for yourselfecho "" >> ${clean_log_file} done

?

轉載于:https://www.cnblogs.com/lsdb/p/6775078.html

總結

以上是生活随笔為你收集整理的日志文件清理代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。