Solved

How to truncate log files in solaris

Posted on 2007-11-27
9
5,001 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

919 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

17 Experts available now in Live!

Get 1:1 Help Now