Solved

How to truncate log files in solaris

Posted on 2007-11-27
9
5,174 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

719 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