对于产生大量日志的服务器,要对日志进行控制,如果任由日志膨胀可能会写满磁盘造成服务器宕机。
要提的是代码中几个值得注意的地方:
命令 | 说明 |
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脚本会因回车换行问题致使执行时一直报错,参看“”
#code by ls in 20170427date_now=`date +"%Y-%m-%d"`date_yes=`date -d "1 day ago" +"%Y-%m-%d"`#the drectory of this script,need to change for yourselfclean_log_path="/iom_jk/IOM_INF/tomcat_6.0.44_1/clean_log/"if ! [ -e ${clean_log_path} ]then mkdir -p $clean_log_pathficlean_log_file="${clean_log_path}clean_log.log" #the logfile of this script,need to change for yourselfdelete_log_prefix="log.log." #the prefix of delete file,need to change for yourselfzero_log_postfix=".log" #the postfix of zero file,neet to change for yourself#those drectory that need to clean,neet to change for yourselfarray_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 general if [ -e ${delete_log_prefix}${date_yes} ] then rm -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 general for file_name in `ls *${zero_log_postfix}` do echo "" > ${file_name} && echo "${data_now} : ${file_name} have been clean" >> ${clean_log_file} done}for path_log in ${array_path[*]}do cd $path_log echo "$date_now : enter ${path_log}" >> ${clean_log_file} delete_log_file #the function to delete yesterday log file,need to change for yourself zero_log_file #the function to zero log file,need to change for yourself echo "" >> ${clean_log_file}done