Linux上清理oracle listener.log文件脚本

Linux上清理oracle listener.log文件脚本,配合crontab定时清理大于1000MB的listener.log
依赖于 lsnrctl status 不指定 LISTENER 可以获取信息。

#!/bin/bash
# This script is used to clean up Oracle Database listener.log on Linux
# Please use the Oracle DB installation user(such as oracle) to execute the script
# Originally written by Xander.Cui
# Use two methods to obtain the absolute path of listener.log, only for detection.
# 2. Obtain the standard listener.log file name
# export listenerlog=$ORACLE_BASE/diag/tnslsnr/$(hostname)/listener/trace/listener.log
# export listenerlogbak=$listenerlog-$DateMark
source ~/.bash_profile
export DateMark=$(date +'%Y%m%d')
export DT=$(date +'%F %T')
# Check if the log directory exists, create it if it does not exist
export logdir=~/log
export checklog=$logdir/listenerlog_check.log
if [ ! -d $logdir ]; then
    mkdir -p $logdir
    echo "
    $DT Checked: Created $logdir
    " >$checklog
fi
# 1. Obtain listener.log through lsnrctl status
export xmlfile=$(lsnrctl status | grep "log.xml" | awk '{print $4}')
export prefixlog=${xmlfile%%/alert*}
export listenerfile=$prefixlog/trace/listener.log
export listenerfilebak=$listenerfile-$DateMark
# Calculate the size of the listener.log file in MB, Use the file name obtained by method 1
export logsizemb=$(du -sm $listenerfile | awk '{print $1}')
# Operating listener.log file
if [ $logsizemb -gt 1000 ]; then
    echo "*************** Moved **************
    $DT Checked: Backup Logfile $listenerfile to $listenerfilebak.tar.gz
    " >>$checklog
    mv $listenerfile $listenerfilebak;tar -zcPf $listenerfilebak.tar.gz $listenerfilebak --remove-files
else
    echo "$DT Checked: Logfile($listenerfile) Size < 1000 MB, Do nothing." >>$checklog
fi
# End

使用命令添加定时任务

(crontab -l | grep -v check_listenerlog) | crontab
(crontab -l ; echo "0 18 * * * bash ~/scripts/check_listenerlog.sh") | crontab

Backup

#!/bin/bash
# This script is used to clean up Oracle Database listener.log on Linux
# Please use the Oracle DB installation user(such as oracle) to execute the script
# Originally written by Xander.Cui
# Use two methods to obtain the absolute path of listener.log, only for detection.
# 2. Obtain the standard listener.log file name
# export listenerlog=$ORACLE_BASE/diag/tnslsnr/$(hostname)/listener/trace/listener.log
# export listenerlogbak=$listenerlog-$DateMark
source ~/.bash_profile
export DT=$(date +'%F %T')
# Check if the log directory exists, create it if it does not exist
export logdir=~/logs
export checklog=$logdir/listenerlog_check.log
if [ ! -d $logdir ]; then
    mkdir -p $logdir
    echo "
    $DT Checked: Created $logdir
    " >$checklog
fi
# 1. Obtain listener.log through lsnrctl status
export xmlfile=$(lsnrctl status | grep "log.xml" | awk '{print $4}')
export prefixlog=${xmlfile%%/alert*}
export listenerfile=$prefixlog/trace/listener.log
export listenerfilebak=listener.log.bak
# Calculate the size of the listener.log file in MB, Use the file name obtained by method 1
export logsizemb=$(du -sm $listenerfile | awk '{print $1}')
# Operating listener.log file
if [ $logsizemb -gt 1000 ]; then
    echo "
    *************** Moved **************
    $DT Checked: Backup Logfile $listenerfile to $listenerfilebak.tar.gz
    " >>$checklog
    tail -20000 $listenerfile >$listenerfilebak;>$listenerfile
else
    echo "$DT Checked: Logfile($listenerfile) Size < 1000 MB, Do nothing." >>$checklog
fi
# End

你可能感兴趣的:(Linux上清理oracle listener.log文件脚本)