📄 cm_log_0004.ksh
字号:
#!/bin/ksh#(@) 日志文件清理服务,注意,该功能不清理上传OMS服务器的文件。# 本程序调用不需要参数# 载入 HPUX 实例定义的环境变量. $HOME/hpux/.setenv# 载入 OMS 系统为 HPUX 实例下传的环境信息. $HOME/hpux/config/ftp.cfg# 载入本任务定义的环境变量. $HOME/hpux/src/CM_LOG_0004/fun.cfg# 载入 公共函数部分。常用的函数定义在该文件中. $HOME/hpux/src/COMMON/common.ksh# 切换到日志路径 $LOGPATHcd $LOGPATHlogit INFO "${FUN_NAME}开始{"######################## 判断激活标志是否为1#######################checkActiveFlagif [[ $taskFlag = 1 ]] then logit WARN "${FUN_NAME}任务失效" logit INFO "${FUN_NAME}结束" exit 1elif [[ $agentFlag = 1 ]] then logit WARN "HPUX实例失效" logit INFO "${FUN_NAME}结束" exit 1elif [[ $agentFlag = 2 ]] then logit WARN "HPUX实例休眠" logit INFO "${FUN_NAME}结束" exit 1fiif [[ ! -s $CFGPATH/logcycle.cfg ]] then # 如果输入文件为空,采取什么动作 0 -- 报告错误返回1 -- 默认将全部数据取出,生成输出文件 # 根据本服务的实际情况,必须存在输入配置文件,因此 NULLDEFAULTFLAG = 0 NULLDEFAULTFLAG = 0 if [[ $NULLDEFAULTFLAG = 0 ]] then headline "000603" "配置文件 $CFGPATH/logcycle.cfg 不存在" logit "WARN" "配置文件 $CFGPATH/logcycle.cfg 不存在" logit INFO "${FUN_NAME}结束}" fputhead exit fifi# 根据定义的日志清理周期,过期的日志备份删除,注意是从原始文件删除,原始文件其他部分保留,只删除过期的部分# $CFGPATH/logcycle.cfg 每个任务的输出文件保存周期列表while read -r trcd cycle restdo if [[ ${#trcd} = 0 ]] then continue fi head=`expr "$trcd" : '^\(.\)'` # head=`expr substr "$trcd" 1 1 ` if [[ $head = "#" ]] then continue fi # logit DEBUG "$trcd $cycle $rest" # 如果没有定义,则清理默认时间的日志 # LOGKEEPDAYS定义在 hpux/.setenv 中 if [[ ${#cycle} = 0 || ${cycle} = 0 ]] then cycle=$LOGKEEPDAYS logit DEBUG "使用默认的日志保存时间 $LOGKEEPDAYS" fi logfile=${trcd}.log if [[ ! -s $logfile ]] then logit INFO "$logfile 文件不存在,不需要清理" continue fi # cycle 保存天数 (( cycle = cycle - 1 )) # 取本日字符串 today=`date +'%Y%m%d'` # 得到不被清理日志的最早的日期。 该日期前的日志都被清理 before_date $today $cycle | read -r markdate logit DEBUG "markdate is $markdate" # 取被清理日志的最后一天 yesterday $markdate | read -r taildate logit DEBUG "taildate is $taildate"# if [[ `grep -e "^\[${markdate}" $logfile >/dev/null 2>&1` != 0 ]] then grep -e "^\[${markdate}" $logfile >/dev/null 2>&1 if [[ $? != 0 ]] then logit INFO "$logfile 没有日志需要清理" continue fi # 先保存被删除部分的日志,然后把需要删除的日志删除。 ex $logfile <<! >/dev/null 2>&1 1,/^\[${markdate}/ -1 1,. w! ${logfile}.${taildate} 1,. d x! if [[ -s ${logfile}.${taildate} ]] then logit INFO "$LOGFILE 中 $markdate 之后的日志保留" logit INFO "$LOGFILE 日志清理完毕,备份文件为 ${LOGFILE}.${taildate}" fi done < $CFGPATH/logcycle.cfglogit INFO "本次日志清理成功"# 构造本次任务的结果文件headline 000000 "本次日志清理成功"# 将本次任务的结果文件发送到 OMS 服务器cd $OUTPATHecho fputheadfputheadlogit INFO "${FUN_NAME}结束}"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -