Solved

How to truncate log files in solaris

Posted on 2007-11-27
9
5,040 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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.

810 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