shell 切割脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell 切割脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash

#return error code
#0: success
#1: parameter error
#6: custom.log file is empty
#7: cut  file error
#8: up log file success

TAR_DIR_FILE_MAX=30
MAIN_LOG_FILE_MAX=1024
MAIN_LOG_NAME=‘custom.log‘
CUT_MAIN_LOG_FILE="custom_$(date +"%Y%m%d%H%M").log"

function usage()
{
   echo "Options:"
   echo "       -f: flg"
   echo "       -t: custom.log tar path"
   echo "       -l: custom.log path"
   echo "   -h: show help info"
}
function check_parameter()
{
   test "x${LOG_FLG}-${TAR_PATH}-${MAIN_LOG_PATH}" = "x--" && echo "Error: argment is empty" && usage && exit 1
   test "x$LOG_FLG" != "x1" && echo "Error: -f flg argment error" && usage && exit 1
   test "x$TAR_PATH" = "x" && echo "Error: -t custom.log tar path" && usage && exit 1
   test "x$MAIN_LOG_PATH" = "x" && echo "Error: -l custom.log path" && usage && exit 1
   [ -f "$MAIN_LOG_PATH" ] && echo "Error: -l must is dir" && usage && exit 1
   [ -f "$TAR_PATH" ] && echo "Error: -t must is dir" && usage && exit 1
}
function touch_main_file()
{
  touch "${MAIN_LOG_PATH}/${MAIN_LOG_NAME}"
  chmod 777 "${MAIN_LOG_PATH}/${MAIN_LOG_NAME}"
}
function rm_tar_file()
{
   SUM_TAR_FILE=`ls ${TAR_PATH}/ |wc -l`
   if [ "$SUM_TAR_FILE" -gt "$TAR_DIR_FILE_MAX" ]; then
      `find ${TAR_PATH}/ -mtime +2|xargs rm -f`
   fi
}

while getopts :f:t:l: OPTION
do
    case $OPTION in
       f)
       LOG_FLG=$OPTARG
        ;;
       t)
       TAR_PATH=$OPTARG
            ;;
       l)  MAIN_LOG_PATH=$OPTARG
        ;;
       h)
        usage
        exit 0
        ;;
       \?)
        echo "-$OPTARG: invalid option"
        usage
        exit 1
        ;;
    :)
        echo "-$OPTARG: miss option argment"
        usage
        exit 1
        ;;
    esac
done
#### 检查参数
check_parameter
#### 切割
[ ! -f "${MAIN_LOG_PATH}/${MAIN_LOG_NAME}" ] && touch_main_file
[ ! -s "${MAIN_LOG_PATH}/${MAIN_LOG_NAME}" ] && echo "${MAIN_LOG_FILE}/${MAIN_LOG_NAME} file is empty" && exit 6
mv ${MAIN_LOG_PATH}/${MAIN_LOG_NAME}   ${MAIN_LOG_PATH}/${CUT_MAIN_LOG_FILE}
test "x$?" != "x0" && echo "cut ${MAIN_LOG_PATH}/${MAIN_LOG_NAME} file error" && exit 7
touch_main_file
#### 打包
tar cjfP  ${TAR_PATH}/${CUT_MAIN_LOG_FILE}.tar.bz2  ${MAIN_LOG_PATH}/${CUT_MAIN_LOG_FILE}
#### 上报
FILE_SIZE=`du ${MAIN_LOG_PATH}/${CUT_MAIN_LOG_FILE}|awk ‘{print $1}‘`
[ "$FILE_SIZE" -gt "$MAIN_LOG_FILE_MAX" ] && UNIQ_FILE=`cat ${MAIN_LOG_PATH}/${CUT_MAIN_LOG_FILE}|sort|uniq|head -50` || UNIQ_FILE=`cat ${MAIN_LOG_PATH}/${CUT_MAIN_LOG_FILE}|sort|uniq`
#### 删除
rm -f ${MAIN_LOG_PATH}/${CUT_MAIN_LOG_FILE}
#### 删除tar目录下最旧的文件
rm_tar_file
echo -e "${FILE_SIZE}(k)\n${UNIQ_FILE}" && exit 8


======= php

<?php
class sslog {
  public function init(){
    while(true){
     sleep(60);
     $output = array();
     $cmd = "/script/sslog/sslog.sh -l /home/www/Logs -t /home/www/Logs/logtar -f 1 </dev/null 2>&1 ";
     exec($cmd, $output, $code);
     if($code==6){
       continue;
     }
     $this->callbackLog(array(
         ‘service_keyword‘ => ‘boss_system_alarm‘,
         ‘rule_keyword‘ => ‘boss_system_alarm‘,
         ‘title‘ => ‘boss system  error‘,
         ‘content‘ => implode(‘,‘, $output)
      ));
    }
  }
  private function callbackLog($logData){
      $postKey   = md5(date(‘Ymd‘) );
      $postUrl   = "http://api.com/tigger_subscribe";
      $postData  = json_encode(array_merge(array(
            ‘fctoken‘ => $postKey,
            ‘fcname‘ => ‘syslog_monitor‘,
        ),$logData));
       $res = $this->postCurlData($postUrl,$postData);
       if(is_null(json_decode($res))){
          echo ‘callback url error....‘;
       }else{
         $decodeRes = json_decode($res, true);
         if(isset($decodeRes[‘status‘]) && !empty($decodeRes[‘status‘])){
            echo "callback url--{$res}";
         }
       }
       return $res;
   }
   private function postCurlData($get_url, $postdata = ‘‘, $other_options = array()) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $get_url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        if (!empty($other_options[‘userpwd‘])) {
          curl_setopt($curl, CURLOPT_USERPWD, $other_options[‘userpwd‘]);
        }
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($curl);
        if (curl_errno($curl)) {
            return false;
        }
        curl_close($curl);
        return $ret;
   }
}
$sslog = new sslog();
$sslog->init();

本文出自 “开发与运维” 博客,谢绝转载!

以上是关于shell 切割脚本的主要内容,如果未能解决你的问题,请参考以下文章

Shell脚本实现 tomcat 日志定时切割

shell脚本:日志切割与上传

linux shell:nginx日志切割脚本

Linux shell脚本按日期 切割pm2日志

tomcat 日志切割shell脚本

Apache日志切割shell脚本