How to clear log files periodically?

13,087

There is a tool called logrotate read the manual page man logrotate it is started by cron and probably already cleaning the log directory you can add your own configurataion files to /etc/logrotate.d

Share:
13,087

Related videos on Youtube

mahesh
Author by

mahesh

Updated on September 18, 2022

Comments

  • mahesh
    mahesh almost 2 years

    I am new to shell scripting. I want to clear log files in a directory based on the dates. I have managed to gather this code:

    DIR1='/home/u01/app/oracle/servers/DIR1/logs'
    DIR2='home/u01/app/oracle/servers/DIR2/logs'
    DIR3='home/u01/app/oracle/servers/DIR3/logs'
    Admin='/home/u01/app/oracle/fmw/admin_poc001/domain/domain_IAM_POC01/servers/AdminServer/logs'
    
    echo "Enter the path"
    read path
    if [ $path == $DIR1 || $path == $DIR2 || $path == DIR3 || $path == $Admin ]
    then
      echo  $path "check your path"
    
    find $path -mtime +10 --# This line here "10" represents listing out the last 10 days logs
    for i in *.log* *.out*
    do
      cat /dev/null >"$i"
    done
    else
    echo "Re-enter the Correct path"
    read path
    fi
    

    This script gives me output when I give input like this:

    Enter your path :
    /home/u01/app/oracle/servers/DIR1/logs
    

    It will clear all the log files until the date of running that script.

    But I want that if I run this script I can clear the log files for times other than the last 10 days, i.e., if I run this today (24/06/2015), then I can clear the log files before the date 14/06/2015.

    Also: rather than entering the full directory path, can I store that in a variable and just type that variable name (instead of entering the full directory path)?

    For example, instead of entering /home/u01/app/oracle/servers/DIR1/logs I am looking for a procedure where I can just enter DIR1 so that it can go into that directory without the total path.

  • ctrl-alt-delor
    ctrl-alt-delor about 9 years
    probably not configured to do the ones in home directory, but you can run it manually, or add configuration.
  • Abel Cheung
    Abel Cheung about 9 years
    Note that logrotate may or may not be available on the concerned OS, which is not stated clearly in OP.
  • mahesh
    mahesh about 9 years
    Hi thanks for response but can you please elaborate about this, as i am very new to shell scripting , what are the configuration files and i need to clear all *.log files and *.out files based on the date and day which has to clear the log files accordingly.thanks in advance. please share an example script if anyone has
  • X Tian
    X Tian about 9 years
    have you read the manual page for logrotate yet ? whenyou try something and get stuck, write a new question.