This post is also available in: Português (Portuguese (Brazil))

Hello all!

Hope you are OK!

Some years ago I’ve developed a simple shell script to purge old trace files and also to compact listener log.

There is only one pre-requisite: this script only works when you are not using OS user segregation. I mean, all the products should have the same owner.

I just have implemented this script in a lot of customers that I’ve worked in the past, but, it always good to reminder: ALWAYS test before implement in a Production system.

This script is dynamic, so, it will obtain all Oracle Homes from oraInventory.

The name of script is purge_oracle_files.sh:

#!/bin/bash

# Obtain Oracle Inventory Location and create a file

rm -f oraInventory.loc 2>/dev/null
inventory_location=`cat oraInventory.loc`
cat /etc/oraInst.loc | grep "loc" | cut -d "=" -f2 >> $inventory_location


# Creates the log dir

mkdir log 2>/dev/null

# Obtain Oracle Home Names and Locations and create a separated file for each Oracle Home
rm -f oracle_homes *.oh 2>/dev/null
cat ${inventory_location}/ContentsXML/inventory.xml | grep "HOME NAME" | awk '{print $2" "$3}' | grep -v "PLUGIN" |grep -v "agent" | grep -v "REMOVED" | sed 's/NAME=\"//' | sed 's/LOC=\"//' | sed 's/\"//g' >> oracle_homes
cat oracle_homes | while read line; do name=`echo $line | awk '{print $1}'`; touch $name.oh; echo $line | awk '{print $2}' > $name.oh; done

# Convert days in minutes - Required by ADRCI
minutes=$(expr $1 \* 24 \* 60)
logfile=`echo $name.$data.log`


# Create Shell Scripts and Execute them
ls -l *.oh | awk '{print $9}' | while read oh
do
name=`echo ${oh} | cut -d "." -f1`
data=`date +%Y%m%d`
shell=`echo ${oh}.sh | sed 's/.oh//'`
logfile=`echo $name.$data.log`
rm $shell 2>/dev/null
touch $shell
touch log/$logfile
export ORACLE_HOME=`cat $oh`
export PATH=$ORACLE_HOME/bin:/bin:/usr/bin:$PATH
if [ -d "$ORACLE_HOME" ] 
then
LISTENERS=`$ORACLE_HOME/bin/adrci exec="show homes"| grep -v ":" | grep "listener" | grep -v grep | wc -l`
if [[ $LISTENERS -gt 0 ]]
then
ps -ef |grep tns | grep -v grep | grep "LISTENER" | awk '{print $9}' | while read listener
do
echo "lsnrctl <<EOF" >> log_status_off.sh
echo "set current_listener $listener" >> log_status_off.sh
echo "set log_status off" >> log_status_off.sh
echo "EOF" >> log_status_off.sh
done
ps -ef |grep tns | grep -v grep | grep "LISTENER" | awk '{print $9}' | while read listener
do
echo "lsnrctl <<EOF" >> log_status_on.sh
echo "set current_listener $listener" >> log_status_on.sh
echo "set log_status on" >> log_status_on.sh
echo "EOF" >> log_status_on.sh
done
ps -ef |grep tns | grep -v grep | grep "LISTENER" | awk '{print $9}' | while read listener
do
echo "cd" `lsnrctl status $listener | grep "Listener Log" | awk '{print $4}' | sed 's/\/alert\/log.xml/\/trace/g'` >> gzip_listener.sh
echo "gzip *.log" >> gzip_listener.sh
done
$ORACLE_HOME/bin/adrci exec="show homes"| grep -v ":" | grep -v "listener" | while read homepath
do
echo "echo \"Realizando expurgo do Home Path: $homepath\"" >> $shell
echo "$ORACLE_HOME/bin/adrci exec=\"set echo ON; spool log/$logfile append; set homepath $homepath; purge -age $minutes\" >> log/$logfile" >> $shell
#break
done
else
export ORACLE_HOME=`cat $oh` >> $shell
export PATH=$ORACLE_HOME/bin:$PATH >> $shell
echo "export ORACLE_HOME=$ORACLE_HOME" >> $shell
echo "export PATH=$PATH" >> $shell
echo "logfile=log/$logfile" >> $shell
$ORACLE_HOME/bin/adrci exec="show homes"| grep -v ":" | while read homepath
do
echo "echo \"Realizando expurgo do Home Path: $homepath\"" >> $shell
echo "$ORACLE_HOME/bin/adrci exec=\"set echo ON; spool log/$logfile append; set homepath $homepath; purge -age $minutes\" >> log/$logfile" >> $shell
break
done
fi
else
echo "Oracle Home registrado no inventario e inexistente no servidor: $name: $ORACLE_HOME" >> log/$logfile 2>/dev/null
rm $shell 2>/dev/null
fi
chmod +x $shell 2>/dev/null
chmod +x log_status*.sh 2>/dev/null
chmod +x gzip_listener.sh 2>/dev/null
# sh $shell >> log/$logfile 2>/dev/null
sh log_status_off.sh 2>/dev/null
sh gzip_listener.sh 2>/dev/null
sh log_status_on.sh 2>/dev/null
done

 

The execution is very simple:

./purge_oracle_files 10

Where:

10 is the number of days that I would like to retain my trace files.

This rule does not apply to the listener logs. To listener logs, the script always will gzip and create a new one.

Hope this helps!

 

Peace!

 

Vinicius