shell--日志切割脚本

2021-01-16  本文已影响0人  w_dll

日志切割虽然有linux自带的logrotate;
但是个人感觉并不好用,因为要重启服务;
于是用shell写了简单的不需要重启服务的切割脚本;

v2

#!/bin/bash
# crontab : 01 */6 * * *
# info : 6小时分割日志一次
# date : 2021-01-16
split_file(){
  full_log_file=$1
  log_path=${full_log_file%/*}
  log_file=${full_log_file##*/}
  bak_time=$(date '+%Y-%m-%d-%H%M')
  bak_log_file=$log_file'-'$bak_time
  if [ -d $log_path ];then
    cd $log_path
    if [ -f $log_file ];then
      echo $log_file
      echo $bak_log_file
      cp $log_file $bak_log_file && cat /dev/null > $log_file && gzip $bak_log_file
    else
      echo 'not exist!'
    fi
  fi
}

split_file '/home/weblogic10/.pm2/logs/main-error.log'
split_file '/home/weblogic10/.pm2/logs/main-out.log'

v1

#!/bin/bash
# user weblogic
# 55 23 * * * bash /u03/log/ops_batch/split-log.sh > /dev/null

log_path='./'
log_name='test.log'

this_day=$(date '+%Y-%m-%d')
log_file=$log_path$log_name
bak_log_file_pre=${log_name%.*}
bak_log_file=$bak_log_file_pre'-'$this_day'.log'

if [[ -f "$log_file" ]];then
    cd $log_path
    cp $log_name $bak_log_file && cat /dev/null > $log_name && gzip $bak_log_file
fi
上一篇 下一篇

猜你喜欢

热点阅读