shell 指令碼作業記錄通用模組,shell日誌

來源:互聯網
上載者:User

shell 指令碼作業記錄通用模組,shell日誌
目標

實現記錄SHELL執行的開始時間,結束時間,運行狀態,錯誤資訊等,以函數封裝日誌記錄的方式,指令碼調用函數

源碼通用函數指令碼program_log_new.sh
function init_log(){sqlplus -S test/passw0rd@orcl <<EOFinsert into program_log values($id,$day,'$1', sysdate,null,'S',null);commitexitEOF}function modify_status(){sqlplus -S test/passw0rd@orcl <<EOFupdate program_log set program_status='$1',end_date=sysdate where id=$id;commitexitEOF}function exception_write(){if [ $? -ne 0 ]then modify_status $status2exit 1fi}function finish_write(){if [ $? -eq 0 ]then modify_status $status1#modify_status $1 $2 $3 else#  modify_status $1 $2 $3 modify_status $status2exit 1fi}status1=Cstatus2=Fday=`date "+%Y%m%d"`id=`sqlplus -S user/1234@test <<EOFset heading offselect  program_log_seq.nextval from dual;commitexitEOF`
#!/bin/sh. ~/.bash_profilesource program_log_new.sh    //通用指令碼init_log sh_xx                                            //初始化日誌函數調用,傳入程式名shell命令xxx 2>${logdir}/xx_$time.log          exception_write                                       //發生異常,調用異常,程式退出shell命令xxx 2>${logdir}/xx_$time.log          exception_write                                       //發生異常,調用異常,程式退出....shell命令xxx 2>${logdir}/xx_$time.log          finish_write                                       //發生異常,調用異常,程式退出




shell指令碼怎產生作業記錄

自己寫的指令碼調用的日誌列印函數,供參考

在指令碼開頭的工作
定義記錄檔LOGFILE
定義記錄序號檔案_LOGSEQ

定義日誌函數
log()
{
#檢查是否存在記錄檔,如果存在,則檢查檔案是否過大(20M)
#過大時,切換檔案,並將目前的記錄序號儲存在_LOGSEQ中。
if [ -f $LOGFILE ];then
LogFileLen=`ls -l ${LOGFILE} | awk '{print $5}'`
if [ $LogFileLen -gt 20971520 ]; then
if [ -f ${_LOGSEQ} ] ; then
_OrgSeq="`cat ${_LOGSEQ}`"
if [ $_OrgSeq -gt 98 ];then
LogFileSeq=0
else
LogFileSeq=`expr ${_OrgSeq} + 1`
fi
else
LogFileSeq=0
fi
echo "${LogFileSeq}" > ${_LOGSEQ}
mv $LOGFILE ${LOGFILE}.${LogFileSeq}
fi
fi
_LogInfo=$1
echo `date +20'%y-%m-%d %H:%M:%S'`" ${_LogInfo} " >> ${LOGFILE} 2>&1
}

需要打日誌時調用log函數即可
 
shell指令碼分析記錄檔

你到是給我一段你的日誌啊。具體要分析什麼。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.