Common shell script running Log Module, shell log
Target
Implement recording of SHELL execution start time, end time, running status, error information, etc. Use the function to encapsulate log records and use scripts to call functions.
Source code universal function script 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 // public script init_log sh_xx // initialize the log function call and input the program name shell command xxx 2 >$ {logdir}/xx _ $ time. log exception_write // exception occurred. The call is abnormal. The program exits the shell command xxx 2 >$ {logdir}/xx _ $ time. log exception_write // exception occurred. The call is abnormal and the program exits .... shell Command xxx 2 >$ {logdir}/xx _ $ time. log finish_write // exception occurred. The call is abnormal and the program exits.
How does a shell Script Generate a running log?
The log printing function called by a self-written script for your reference.
Work starting with Script
Define Log File LOGFILE
Define log serial number file _ LOGSEQ
Define log Functions
Log ()
{
# Check whether a log file exists. If yes, check whether the file is too large (20 mb)
# If it is too large, switch the file and save the current log serial number in _ 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
}
Call the log function when logging is required.
Shell script to analyze log files
You just gave me a piece of your log. What to analyze.