LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-11-2009, 10:32 AM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
log script


Hello guys,

Does anyone have a sample script that if a log file gets greater then 10MB and deletes it?

Thanks

Last edited by cmontr; 06-11-2009 at 10:33 AM.
 
Old 06-11-2009, 10:37 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have at least three possibilities:
1) use the find command with -size and -exec.
2) use the stat command with -c%s and if the size is greater than a certain value delete the file
3) add a rule to logrotate and specify the size option.
 
Old 06-11-2009, 10:56 AM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
This will be in Solaris box...by any chance do you have a sample one that I can modify?
Thanks


Quote:
Originally Posted by colucix View Post
You have at least three possibilities:
1) use the find command with -size and -exec.
2) use the stat command with -c%s and if the size is greater than a certain value delete the file
3) add a rule to logrotate and specify the size option.
 
Old 06-11-2009, 11:09 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
find . -name logfile -size +10000000c -exec echo rm {} \;
Note than on Solaris the -size option accepts either a number of 512-bytes blocks or the size in bytes (using the c suffix as in my example). The echo statement is just for testing purpose (remove it if you want to actually delete the file).
 
Old 06-11-2009, 11:26 AM   #5
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
This works, thank you very much.
find . -name test.log -size +10c -exec cat /dev/null >test.log {} \;


Is there a way to add a condition to it such as say:

if test.log > 10MB
then keep last 2MB logged
then delete the rest



Thanks much


Quote:
Originally Posted by colucix View Post
Code:
find . -name logfile -size +10000000c -exec echo rm {} \;
Note than on Solaris the -size option accepts either a number of 512-bytes blocks or the size in bytes (using the c suffix as in my example). The echo statement is just for testing purpose (remove it if you want to actually delete the file).
 
Old 06-11-2009, 11:51 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well.. in that case I'd use split and a simple script like this:
Code:
#!/bin/sh
size=`ls -l logfile | awk '{print $5}'`
if [ $size -gt 10000000 ]
then
  tail -r logfile > reversed_logfile
  split -b 2m reversed_logfile
  tail -r xaa > logfile
  rm reversed_logfile x??
fi
The tail -r command is to be sure you get the last 2 mb of the file, otherwise the last part of the original splitted file may contains only few kb.
 
Old 06-11-2009, 01:02 PM   #7
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
tried this script...it runs but something not working. Could you explain what xaa and x?? supposed to be? I modified few times but not getting results. Thanks again.


Quote:
Originally Posted by colucix View Post
Well.. in that case I'd use split and a simple script like this:
Code:
#!/bin/sh
size=`ls -l logfile | awk '{print $5}'`
if [ $size -gt 10000000 ]
then
  tail -r logfile > reversed_logfile
  split -b 2m reversed_logfile
  tail -r xaa > logfile
  rm reversed_logfile x??
fi
The tail -r command is to be sure you get the last 2 mb of the file, otherwise the last part of the original splitted file may contains only few kb.
 
Old 06-12-2009, 02:19 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, xaa, xab, xac... are the default name of the pieces created by the split command. Go through the man page of these command is something is not clear or if some behaviour is different. I have tested it on a Solaris Sparc 5.8 and it works.

Anyway, please post the exact commands you tried and copy/paste the error messages. If you simply tell "something don't work".... you tell me nothing!
 
  


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
script to log users in zerocool22 Linux - Server 3 05-13-2008 01:59 AM
start script during log off jpostma Slackware 7 07-20-2007 04:43 PM
Bash script for server log (namely var/log/messages) tenaciousbob Programming 17 05-24-2007 10:43 AM
Run this script after I log out lothario Linux - Software 2 06-03-2006 09:07 PM
log cleanup script teacup Programming 2 09-24-2003 11:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:23 PM.

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