Solved

How to truncate log files in solaris

Posted on 2007-11-27
9
4,976 Views
Last Modified: 2013-12-27
Hello,

I'm wondering how to truncate the log files (like /var/adm/messages or utmpx) in solaris? Is there a way to keep the log file size to a particular size limit?

Thanks,
Ashok
0
Comment
Question by:rdashokraj
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 200 total points
ID: 20356515
man logadm

and

cat /etc/logadm.conf
0
 
LVL 14

Expert Comment

by:arthurjb
ID: 20358273
If it is a log file you don't care about and never need it back, here is the simple way;

> name-of-file.log

(Yes it is just a greater than sign, the logfile name, and press return.)

It is important that you do not just delete the file, since the logging programming could continue to write in a way that you cannot easily see the file that the info is going to, and it will eventually fill up the disk...
0
 
LVL 22

Expert Comment

by:blu
ID: 20360689
The problem with truncating the file with the redirect is that not all programs have logfiles open with
append. It is not uncommon that they just open it for writing and that the file pointer is left as is after
each write. Thus, after you truncate the file, the next write will write to the same position it would have
before, creating a sparse file with a huge number of nulls at the beginning.
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 20363943
You can do this I think:
From the program which is writing the logs, where ever it is writing to the log, first check the no. of lines it has in it. And if the number of lines is greater than a given value of maximum lines, replace that file else append to that file.

For example if your logfile is LOG.TXT then we can write something like this before writing to the file:

lines=`wc -l LOG.TXT | awk '{ print $1 }'`
if [[ $ lines -gt $maxLines ]]
then
 echo "Lines for log" > LOG.TXT
else
 echo "Lines for log" >> LOG.TXT
fi

And if you don't have access to the file generating the output (means you cannot change that) then you can write a script which will periodically check the size and reset the file whenever required:
lines=`wc -l LOG.TXT | awk '{ print $1 }'`
if [[ $ lines -gt $maxLines ]]
then
echo > LOG.TXT
fi

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:vikaskhoria
ID: 20363951
Also do you don't want to change the file to zero size everytime??
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20364181
UUOA ;-)

lines=`wc -l LOG.TXT | awk '{ print $1 }'`

lines=`wc -l <LOG.TXT`

0
 
LVL 6

Assisted Solution

by:Talmash
Talmash earned 50 total points
ID: 20365193
split -l 10000 large_file <output_file_new_extension>

example:
abc has 20005 lines,
xaa will hold lines 1-10000
xab will hold lines 10001-20000
xac .. 20001-20005
0
 

Author Comment

by:rdashokraj
ID: 20365204
Thanks for all your solutions. However the solution given by Tintin seems to be appropriate one and it satisfies my requirement. Thanking you all once again.
0
 

Author Comment

by:rdashokraj
ID: 20365211
Thanks for all your solutions. However the solution given by Tintin seems to be appropriate one and it satisfies my requirement. Thanking you all once again.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now