LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2010, 08:31 PM   #1
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Rep: Reputation: 0
delete old logs


Hi


Hpunix.
I have logfiles in /archive_store directory which are deleted by below script once the directory is 90 percent full.
What I want to do is, before deleting each logfile, I want to make sure they are at least 1 day older than current time.
If they are not, the script should terminate.
Time of the each log can be seen before ".Z" prefix.
I have to pickup that time and compare it with current time.



I aprreciate any help.


----------------------------------------------------
ls /archive_store

akprd_uwprod_0000015702_1_201011121305.Z
akprd_srprod_0000099047_1_201011121305.Z
...
...
...

----------------------------------------------------
The script is:

#!/bin/sh

LOGDIR=/usr/logs/
LOG=trim_archive_store.log
DIR=/archive_store
threshold=${1:-90}

cd ${DIR}

count=$(ls | grep -c '^ak')
if [ $count -eq 0 ]; then
exit
fi
percent=$(df -k ${DIR} | awk '/allocation used/ {print $1;}')
while [ ${percent} -ge $threshold ]; do
files=$(ls -1t ${DIR}/ak* | tail -3)
DTIME=$(date +%Y/%m/%d_%X)
for file in $files; do
echo "$DTIME - Deleting File $file" >> $LOGDIR/$LOG
echo "Deleting file $file"
rm ${file}
done
percent=$(df -k ${DIR} | awk '/allocation used/ {print $1;}')
done

Last edited by tjay1983; 11-11-2010 at 08:35 PM.
 
Old 11-12-2010, 08:52 AM   #2
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
You could try:
Code:
TODAY=`date '+%Y%m%d'`
LOGDAY=`echo "$file" | perl -ne 's/.*_(\d{8})[^_]*\.Z$/\1/; print;'`
if [[ `expr $TODAY - $LOGDAY` -lt 1 ]]; then
  exit
fi
Note: I did not compare times, just dates.

Last edited by hda7; 11-13-2010 at 10:21 AM.
 
Old 11-14-2010, 09:13 PM   #3
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Hi hda7,

Thanks for your help.
By using your code, I have written below code, which suits my needs.

What I want to ask is, this code will not be accurate in below scnerio:

LOGDAY=20101129
TODAY= 20101201

Difference is 72, which is not correct, since these are dates.

How can I fix this?




#!/bin/sh


DIR=/archive_store
TODAY=`date '+%Y%m%d'`

cd ${DIR}

file=$(ls -1t ${DIR}/ak* | tail -1)
LOGDAY=`echo "$file" | perl -ne 's/.*_(\d{8})[^_]*\.Z$/\1/; print;'`

if [[ `expr $TODAY - $LOGDAY` -lt 4 ]]; then
echo "Please check logs..."
fi

Last edited by tjay1983; 11-14-2010 at 09:17 PM.
 
Old 11-14-2010, 09:25 PM   #4
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
If all of the files in that directory are subject to deletion then this would work.
Code:
find /archive_store -type f -atime +1 -exec rm {} \;
If all of the files to be deleted have names that end in Z and if there are other files that you need to keep that have names that do NOT end in Z then this would work.
Code:
find /archive_store -type f -atime +1 -name *Z -exec rm {} \;
It's a little simpler than your script.

Note that the find utility will follow all of the subdirectories unless you tell it not do do that using -maxdepth 1.
Code:
find /archive_store -maxdepth 1 -type f -atime +1 -name *Z -exec rm {} \;

Last edited by stress_junkie; 11-14-2010 at 09:45 PM.
 
Old 11-14-2010, 10:43 PM   #5
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Hi

Thanks for your suggestion but I dont want to delete.
The following script suits my needs, however as I mentioned above it doesnt work in below scnerio:

LOGDAY=20101129
TODAY= 20101201

Difference is 72, which is not correct, since these are dates.





#!/bin/sh


DIR=/archive_store
TODAY=`date '+%Y%m%d'`

cd ${DIR}

file=$(ls -1t ${DIR}/ak* | tail -1)
LOGDAY=`echo "$file" | perl -ne 's/.*_(\d{8})[^_]*\.Z$/\1/; print;'`

if [[ `expr $TODAY - $LOGDAY` -lt 4 ]]; then
echo "Please check logs..."
fi
 
Old 11-14-2010, 10:51 PM   #6
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by tjay1983 View Post
Hi
Thanks for your suggestion but I dont want to delete.
The title of your thread is "delete old logs".
 
Old 11-14-2010, 10:57 PM   #7
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Ohh, my mistake.
I should have found a better title.
thanks for your help
 
Old 11-16-2010, 03:14 PM   #8
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
To make a proper comparison, you could get both dates into perl and use a perl script to calculate the date and time difference (perl would be better suited to that than bash).

By the way, if you find a post helpful, click the scales icon to the left of the post.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Logs onelung02 Linux - Software 3 04-09-2014 02:51 AM
Logrotate: Rotate Apache logs, but never delete kenneho Linux - Newbie 2 10-23-2008 06:58 AM
delete kopete logs venki SUSE / openSUSE 1 11-15-2006 01:02 AM
VAR - logs - when to delete High-gain Linux - Newbie 4 02-27-2005 09:54 AM
How to delete message logs in GAIM? Arodef Linux - Software 1 06-24-2004 05:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration