linux系统中定时清理日志实现方法

发布时间:2018-05-15 15:51:48编辑:丝画阁阅读(328)

需求

项目中使用maxwell监听mysql的binlong后,将消息日志打印到log文件.需要类似log4j的形式,记录最近n天的日志

脚本

#!/bin/bashtarget_dir="/home/apprun/maxwell/script"prefix_log="maxwell-"prefix_sh="start-maxwell_"postfix=".out"ipArr=("172.16.32.227"
 "172.20.3.64" "172.20.3.65" 
"172.20.3.71");keep_days=2start_days=$(($keep_days - 1))today=`date 
+%Y_%m_%d`yesterday=`date -d yesterday +%Y_%m_%d`delday=`date -d -2day 
+%Y_%m_%d`dateArr[0]=`date 
+%Y_%m_%d`hitory_days_count=$[$keep_days+1]for 
((i=1;i<=$history_days_count;i++))dodateArr[$i]=`date -d -$i'day' 
+%Y_%m_%d`echo ${dateArr[$i]}donefor i in ${ipArr[*]};doecho $iPID=`ps 
-ef | grep maxwell | grep "$i" | awk '{print $2}'`echo 
"pid="$PID#杀死进程echo "---------------"for id in $PIDdokill -9 $idecho 
"killed $id"doneecho "---------------"#文件迁移file_name=$prefix_log$iecho 
$i;for n in $(seq $keep_days -1 1)dobefore=$[$n+1]if [ -f 
$target_dir/$file_name"_"${dateArr[$n]}$postfix ]; then mv 
$target_dir/$file_name"_"$n$postfix 
$target_dir/$file_name"_"${dateArr[$before]}$postfix; fidoneif [ -f 
$target_dir/$file_name$postfix ]; then mv $target_dir/$file_name$postfix
 $target_dir/$file_name"_"${dateArr[1]}$postfix; fitouch 
$target_dir/$file_name$postfix;#maxwell重启tmpsh=$target_dir"/"$prefix_sh$i".sh"sh
 $tmpshecho "maxwell "$i" sh done"do

关键字