Solved

How to truncate log files in solaris

Posted on 2007-11-27
9
5,126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

739 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