cancel
Showing results for 
Search instead for 
Did you mean: 

Script for deleting old log files in system

Former Member
0 Kudos

Hi,

     This is Deepesh I am trying find some script to delete log files in \var\log\applog\nmon . We usually get this alerts that the file system is almost full and all we do is go to the path and delete old log files. So basically what i am looking for is the script that i can schedule in corn job . This script should keep logs for two months and delete the rest .

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello Deepesh,

You can create a simple unix script using 'find' command.  Update the directory name where you want to delete the files.  I would suggest to test it before u implement it across the systems

Unix Script:

#!/bin/bash

find <directory> -mtime +60 -type f -exec rm {} \;

Regards,

Mudasir.

Former Member
0 Kudos

Hi Mudasir;

There two files named .profile and .sh_history apart from that any file older than that should be deleted .

Regard

Deepesh

Answers (2)

Answers (2)

Reagan
Advisor
Advisor
0 Kudos

Hello

I believe those are nmon files with extension .nmon

In that case you can create a script like this:

find . -xdev -type f -mtime +60 -name "*.nmon" -exec rm {} \;

To be sure that it is deleting the correct files list the files first using this:

find . -xdev -type f -mtime +60 -name "*.nmon" -exec ls -ltr {} \;

Regards

RB

Former Member
0 Kudos

That's the answer i was looking for . Thanks a lot.

Former Member
0 Kudos

Hi,

I finally did this

#!/bin/sh

###

### Static variables

###

nmon_dir="/var/log/applog/nmon "

cd $nmon_dir

find $nmon_dir -xdev -type f -mtime +120 -name "*.nmon*" -exec rm {} \;

find $nmon_dir -xdev -type f -mtime +5 -name "*.nmon" -exec gzip {} \;

But I am not sure whether that .gz file will be stored .

Reagan
Advisor
Advisor
0 Kudos

Hello

cd $nmon_dir

There is no need to specify cd $nmon_dir in the script as the find command is using the path $nmon_dir which is already defined as a shell varibale.

But I am not sure whether that .gz file will be stored

The files will be zipped in the location where they are present unless you specify another location.

Good Luck

RB

Reagan
Advisor
Advisor
0 Kudos

Hello

nmon_dir="/var/log/applog/nmon"

Modify the script like this:


nmon_dir=/var/log/applog/nmon

Also try to list the files before deleting or zipping them.

Good Luck

RB

Former Member
0 Kudos

Hi,

Firstly which logs are you referring to?

ABAP/JAVA/ AUDIT logs?

Be careful what you delete bu something similar to Mudasir's suggestion can work.

If your reasoning is for space you can use similar command and add

find <directory> -mtime +60 -type f -exec gzip -9{} \;

So insteaf of removing it zips them

Johan

Former Member
0 Kudos

Yes. I will keep that in mind . Thank you .