Script to Delete Log Files Automatically in Oracle

  • by

Trace File, Audit File and Alert Log

In this post I will introduce a shell script that can delete log files automatically. We will handle 3 kinds of log file, i.e. trace files, audit files and alert log in this script, which will retain a certain amount of logs after deleting.

1. Trace File Deletion

I have talked about how to delete Oracle trace files through ADRCI in a recent post to align with our auditing policies in order to manage space in healthy.

Chances are, your disk space might be still under pressure after purging all trace files. Why? This is because you forgot audit files and alert log are still big and growing, which are not controlled by ADRCI.

2. Audit File Deletion

Audit files cannot be deleted by ADRCI, so I use find command to search for candidates and delete them subsequently. For example:

[oracle@primary01 ~]$ find /u01/app/oracle/admin/orcl/adump -type f -mtime +7 -name '*.aud' -exec rm -f {} \;

Which means, we find and remove audit files which were older than 7 days in the audit file dump destination.

3. Alert Log Deletion

As for alert log, you can delete it directly whenever it's urgent, the database will recreate an alert log automatically. In this post, we use a technique to keep the most recent logs by trimming old logs off the alert log so as to control the file size of alert log in a rolling fashion.

Script to Delete Log Files

Suppose we'd like to keep all log files that are within 7 days, we put above all scripts together.

. /home/oracle/.bash_profile

# For deleting obsolete trace files
OBS_IN_MIN=10080 # 7 days
for f in $( adrci exec="show homes" | grep -v "ADR Homes:" );
  echo "Start Purging ${f} at $(date)";
  adrci exec="set home $f; purge -age $OBS_IN_MIN ;" ;

# For deleting obsolete audit files
find /u01/app/oracle/admin/orcl/adump -type f -mtime +7 -name '*.aud' -exec rm -f {} \;

# For trimming alert log
tail -50000 /path/to/alert_ORCL.log > /path/to/alert_ORCL.log.copy; cp -f /path/to/alert_ORCL.log.copy /path/to/alert_ORCL.log; cat /dev/null > /path/to/alert_ORCL.log.copy

You can add this script to crontab for periodically and automatically purging log files.

Please notice the following two things:

  1. The listener log seldom brings you troubles, but you can also add it into the script.
  2. In a RAC environment, you should take grid's log files into account, too.

Leave a Reply

Your email address will not be published. Required fields are marked *