shell 脚本运行日志通用模块,shell日志
目标
实现记录SHELL执行的开始时间,结束时间,运行状态,错误信息等,以函数封装日志记录的方式,脚本调用函数
源码
通用函数脚本program_log_new.shfunction init_log()
{
sqlplus -S test/passw0rd@orcl <<EOF
insert into program_log values($id,$day,'$1', sysdate,null,'S',null);
commit
exit
EOF
}
function modify_status(){
sqlplus -S test/passw0rd@orcl <<EOF
update program_log set program_status='$1',end_date=sysdate where id=$id;
commit
exit
EOF
}
function exception_write(){
if [ $? -ne 0 ]
then
modify_status $status2
exit 1
fi
}
function finish_write(){
if [ $? -eq 0 ]
then
modify_status $status1
#modify_status $1 $2 $3
else
# modify_status $1 $2 $3
modify_status $status2
exit 1
fi
}
status1=C
status2=F
day=`date "+%Y%m%d"`
id=`sqlplus -S user/1234@test <<EOF
set heading off
select program_log_seq.nextval from dual;
commit
exit
EOF`
#!/bin/sh
. ~/.bash_profile
source 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 //发生异常,调用异常,程序退出自己写的脚本调用的日志打印函数,供参考
在脚本开头的工作
定义日志文件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函数即可
你到是给我一段你的日志啊。具体要分析什么。