Operating System - HP-UX
1834528 Members
3275 Online
110068 Solutions
New Discussion

Re: Script to truncats logs

 
Ashley_22
Occasional Contributor

Script to truncats logs

I require a script ( or command) to check a log file and truncate it only if its size is more than 500MB.
4 REPLIES 4
harry d brown jr
Honored Contributor

Re: Script to truncats logs

What do you mean by "truncate"? Do you waht to lop off the top of the log file, the bottom, or cat /dev/null to it and really truncate it?

What kind of log file are we talking about?

truncating log files is complicated, so I suggest LOG ROTATION.

live free or die
harry d brown jr
Live Free or Die
renarios
Trusted Contributor

Re: Script to truncats logs

Hi Asley,

Try this:
#!/bin/sh
#Find all logfiles bigger than 500MB (524288000 bytes) and "flush them"
for i in $(find -name '*.log' -size +524288000c)
do
cat /dev/null > $i
done


cheerio,

Renarios
Nothing is more successfull as failure
Pete Randall
Outstanding Contributor

Re: Script to truncats logs

If you choose to try rotating, here's the tool:

http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/logrotate-2.5/


Pete

Pete
Steven E. Protter
Exalted Contributor

Re: Script to truncats logs

I like logrotate.

Its a standard on Linux and works well for me on those servers.

> name_of_logfile

Will instantly empty any log.

To get that list you run a find command.

find /var -size +500 > /tmp/list

while read -r filename
do
> $filename
done < /tmp/list

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com