不重启服务清理日志文件保留指定大小

#!/bin/bash
#file name, default in current dir
# 删除大文件 路径名称 变量
LAGRE_FILE=$1
 
if [[ ! -f ${LAGRE_FILE} ]];then
 
    echo "${LAGRE_FILE} NOT FOUND, PLEASE CHECK FILE."
    exit 1
 
fi
 
#get file size
FILE_SIZE=`du -shm ${LAGRE_FILE} | awk -F ' ' '{print $1}'`
 
#default 200M 默认每次删除200M空间
DELETE_SPEED=20000
 
#safe rm size 20M default
SAFE_SIZE=5000
 
echo "CURRENT ${LAGRE_FILE} IS ${FILE_SIZE}M, NOW START DELETE..."
 
for ((i=${FILE_SIZE}; i>${SAFE_SIZE}; i=i-${DELETE_SPEED}))
do
 
        echo "truncate ${i}M ......";
        truncate -s ${i}M ${LAGRE_FILE} ;
 
        sleep 2;
done
 
LAST_FILE_SIZE=`du -shm ${LAGRE_FILE} | awk -F ' ' '{print $1}'`
 
echo "CURRENT ${LAGRE_FILE} IS ONLY ${LAST_FILE_SIZE}M, DELETE IT..."
 
# 下面是删除文件动作,可以不要,以免我们的服务无法自动输出到log
# rm -rf ${LAGRE_FILE}
# if [[ ! -f ${LAGRE_FILE} ]];then
#         echo "${LAGRE_FILE} DELTE SUCC."
# else
#         echo "${LAGRE_FILE} DELTE FAILED, PLEASE CHECK AND DELTE IT MANUAL"
#         exit 2
# fi
posted @ 2022-04-20 10:44  哈喽哈喽111111  阅读(173)  评论(0编辑  收藏  举报