把一些服务的日志进行清理:主要是 tomcat日志   nginx的access日志  springboot的jar运行输出日志

主体大致是这样子

找到文件路径,日志复制一份并以时间变量命名,清空旧文件,查找过期文件删除

注意不要直接删除旧文件。进程运行,删除日志文件不会释放磁盘资源。

logs_path=$1
date_now=$(date +%Y-%m-%d)
data_savedays=$2
cp $logs_path/catalina.out $logs_path/catalina.${date_now}.out
>$logs_path/catalina.out;
 find $logs_path -mtime  $data_savedays -name "catalina.*.out" -exec rm -rf {} \;

 

自己瑞色了一下。完整脚本如下:

#!/bin/bash
# -----------------------------------------------------------------
# -------- 创建日期:2022-01                               --------
# -------- 作者:ywjia                                     --------
# -------- 邮箱:duxingren201955@163.com                   --------
# -------- 公司:******************************            --------
# -----------------------------------------------------------------
# 打印log-详细日志-install_all.log
    function Print_info_log(){
        echo -e "\e[1;35m INFO:$(date "+%Y-%m-%d %H:%M:%S") \e[0m $1." | tee -a /install_all.log
    }
# 打印log-错误日志-install_all.log
    function Print_error_log(){
        echo -e "\e[1;38m ERROR:$(date "+%Y-%m-%d %H:%M:%S") \e[0m $1." | tee -a /install_all.log
    }
# 打印log-进度日志-install_show.log
    function Print_show_log(){
        echo -e "\e[1;35m INFO:$(date "+%Y-%m-%d %H:%M:%S") \e[0m $1." | tee -a /install_show.log
    }
    #服务部署的路径
        install_path="/usr/local"
    #文件保留天数
        file_savedays="2"
#清理tomcat的日志文件
function Clear_Tomcat(){
    logs_path=$1
    date_now=$(date +%Y-%m-%d)
    data_savedays=$2
    [ ! -d $logs_path ] && Print_error_log "日志文件目录错误 $logs_path" && exit 2
    [ $# -ne 2 ] && Print_error_log "请完整的输入参数,第一个参数表示日志绝对路径,第二个参数表示日志留存的天数.示例:/data/tomcat_ldh/logs 2 "
    cp $logs_path/catalina.out $logs_path/catalina.${date_now}.out
    >$logs_path/catalina.out;
    find $logs_path -mtime  $data_savedays -name "catalina.*.out" -exec rm -rf {} \;
    find $logs_path -mtime  $data_savedays -name "localhost.*.log" -exec rm -rf {} \;
    find $logs_path -mtime  $data_savedays -name "localhost_access_log.*.txt" -exec rm -rf {} \;
    find $logs_path -mtime  $data_savedays -name "catalina.*.log" -exec rm -rf {} \;
    find $logs_path -mtime  $data_savedays -name "manager.*.log" -exec rm -rf {} \;
    find $logs_path -mtime  $data_savedays -name "host-manager.*.log" -exec rm -rf {} \;
}
#清理jar包留存的日志文件
function Clear_Jar(){
    logs_path=$1
    date_now=$(date +%Y-%m-%d)
    data_savedays=$2
    clear_filename=$3
    [ ! -d $logs_path ] && Print_error_log "日志文件目录错误 $logs_path" && exit 2
    [ $# -ne 3 ] && Print_error_log "请完整的输入参数,第一个参数表示日志绝对路径,第二个参数表示日志留存的天数,第三个参数表示文件名称.示例:/data/logs 2 alg.log "
    cp $logs_path/$clear_filename $logs_path/${clear_filename}.${date_now}
    >$logs_path/$clear_filename;
    find $logs_path -mtime $data_savedays -name "${clear_filename}.*" -exec rm -rf {} \;
}
#清理nginx访问日志
function Clear_Nginx(){
    logs_path=$1
    date_now=$(date +%Y-%m-%d)
    data_savedays=$2
    clear_filename="access.log"
    [ ! -d $logs_path ] && Print_error_log "日志文件目录错误 $logs_path" && exit 2
    [ $# -ne 2 ] && Print_error_log "请完整的输入参数,第一个参数表示日志绝对路径,第二个参数表示日志留存的天数.示例:/data/logs 2 "
    cp $logs_path/$clear_filename $logs_path/${clear_filename}.${date_now}
    >$logs_path/$clear_filename;
    find $logs_path -mtime $data_savedays -name "${clear_filename}.*" -exec rm -rf {} \;
}

#主函数
function main(){
    Print_show_log "开启清理日志服务"
        [ -d ${install_path}/springboot_alg ] && Clear_Jar "${install_path}/springboot_alg" "$file_savedays" "alg.log"
        [ -d ${install_path}/springboot_edp ] && Clear_Jar "${install_path}/springboot_edp" "$file_savedays" "edp.log"
        [ -d ${install_path}/springboot_log ] && Clear_Jar "${install_path}/springboot_log" "$file_savedays" "edplog.log"
        [ -d ${install_path}/springboot_ldv ] && Clear_Jar "${install_path}/springboot_ldv" "$file_savedays" "ldv.log"
        [ -d ${install_path}/tomcat-lw/logs ] && Clear_Tomcat "${install_path}/tomcat-lw/logs" "$file_savedays"
        [ -d ${install_path}/tomcat-ldh/logs ] && Clear_Tomcat "${install_path}/tomcat-ldh/logs" "$file_savedays"
        [ -d ${install_path}/tomcat-lag/logs ] && Clear_Tomcat "${install_path}/tomcat-lag/logs" "$file_savedays"
        [ -d ${install_path}/tomcat-ldv/logs ] && Clear_Tomcat "${install_path}/tomcat-ldv/logs" "$file_savedays"
        [ -d /var/log/nginx ] && Clear_Nginx "/var/log/nginx" "2"
}

#------------start---------
main
#------------end-----------