您的位置:首页 > 其它

定时清理日志

2016-05-05 13:56 183 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/zte619801897/article/details/51322367

脚本:log-clear.sh

#!/bin/bash
cd `dirname "$0"`

GATE_LOGS_DIR="/aaa/bbb/app/Search.Antibot.Gate/logs"

find $GATE_LOGS_DIR \( -name '*.log*' \) -mtime +${log_alive_days:=6} -delete;

# clear the log files that generated by process and greater than 1G,but won't be deleted
for logfile in $( find $GATE_LOGS_DIR \( -name "*.log*" \) -size +1048576k)
do
  echo 'log size more than 1G,just clear it.................' > "$logfile"
done

exit 0

脚本:log-clear-new.sh

#!/bin/bash
cd `dirname "$0"`

if ! . ./env.sh
then
exit 1
fi

find $LOGS_DIR \( -name '*.log*' \) -mtime +${log_alive_days:=29} -delete;

# for start log and GC log, keep only the last 3 days
rm -f $(ls -t ${LOGS_DIR}/start.${app_name}.*.log | tail -n +4)
rm -f $(ls -t ${LOGS_DIR}/${app_name}_gc.*.log | tail -n +4)

# clear the log files that generated by process and greater than 1G, but won't be deleted
for logfile in $( find $LOGS_DIR \( -name "start.*.log" -o -name "${app_name}_gc.*.log" \) -size +1048576k)
do
echo_t 'log size more than 1G,just clear it.................' > "$logfile"
done

exit 0


脚本:env.sh

#!/bin/bash

. ./global.sh

if readlink -f "$0" > /dev/null 2>&1
then
script_name=`readlink -f "$0"`
else
script_name="$0"
fi

local_bin_dir=`dirname "$script_name"`
LOGS_DIR="$local_bin_dir/../../logs"

app_name="myapp"




步骤:

1.修改

vim log-clear.sh

2.转格式

dos2unix log-clear.sh

3.加权限

chmod +x log-clear.sh

4.设置定时清理

修改,对应脚本存放的具体路径
crontab -e
0 5 * * * /home/deploy/bin/log-clear.sh
0 5 * * * /aaa/bbb/ccc/MyProject/work/bin/log-clear.sh
查看
crontab -l
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: