1.初始化日志文件。对文件的大小进行判断和压缩
LOG=$1 if [ -e "${LOG}" ];then LOG_SIZE=`ls -l "${LOG}"| awk '{print $5}'` if [ "${LOG_SIZE}" -gt 10485760 ];then LOG_SUFFIX="."`date +%Y%m%d`".gz" gzip "${LOG}" -S ${LOG_SUFFIX} fi fi
2.写日志
PS=$$ LOG=/home/centos/1.log function LOG() { echo -e "[ `date +'%y-%m-%d %T'`] [$PS]:$*" | tee -a "${LOG}" } LOG "error" "BATCH head procedure is failed"
[centos@s202 ~]$ sh test5.sh [ 19-05-23 14:59:51] [3028]:error BATCH head procedure is failed [centos@s202 ~]$ cat -n 1.log 1 [ 19-05-23 14:59:51] [3028]:error BATCH head procedure is failed