- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to truncate Log file to contain one month data...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2010 12:50 AM
тАО08-15-2010 12:50 AM
How to truncate Log file to contain one month data only
I have a log file which is growing to almost 2 GB in size. I need to truncate this log file to contain only one month old data in order to reduce its size.
Please help in giving a procedure for this.
Regards,
Robert Peregrin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2010 06:44 AM
тАО08-15-2010 06:44 AM
Re: How to truncate Log file to contain one month data only
to truncate you 0 you just need to give a > infront of your file.
>filename
to truncate file with condition then you need to write a script.
Suraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2010 08:28 AM
тАО08-15-2010 08:28 AM
Re: How to truncate Log file to contain one month data only
It also depends on what is the date string in the file. And whether you need exactly 31 days or just the current month or you are only going to run the script on the last/first day of the month.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2010 05:05 PM
тАО08-15-2010 05:05 PM
Re: How to truncate Log file to contain one month data only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-15-2010 11:09 PM
тАО08-15-2010 11:09 PM
Re: How to truncate Log file to contain one month data only
If you want to trim the file , by reducing contents from the log file you can do so by removing first few lines,
1. count the lines, with wc -l logfile ,
2. trim it as desired.
- If you want a gzip copy of backup of the file elsewhere you can keep a copy.
-Example: To trim first 2000 lines from the file:
- stop the process writing the file.
- trim
# perl -i -ne 'print unless 1 .. 2000' logfile.log
- start the process, writing to it.
- check size/lines.
Hth,
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 12:19 AM
тАО08-16-2010 12:19 AM
Re: How to truncate Log file to contain one month data only
Create a script to switch and compress the log file.
- Stop application
- mv logfile to logfile.old
- Initialize new logfile
- Start application
- compress the old logfile
schedule (cron, ovo, ...) the script in specified dates and times.
rgs,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 02:17 AM
тАО08-16-2010 02:17 AM
Re: How to truncate Log file to contain one month data only
The log file is a text file.
The SAM utility is nice but we need the triming to be automatic. A simple script would be useful which I can run as a cron job.
We are currently using the below script. If you can have better and easier suggestions, then it would be helpful.
#!/usr/bin/sh
#
# this script should be run (via cron) daily to keep DMC log at a
# reasonable size
#
WHOAMI=/usr/bin/whoami
ECHO=/usr/bin/echo
CP=/usr/bin/cp
DATE=/usr/bin/date
CAT=/usr/bin/cat
ED=/usr/bin/ed
LOGGER=/usr/bin/logger
DMCLOGDIR=/var/opt/dmc/logs
DMCLOG=dmc.log
#
# make sure script is running as root
#
if [ `$WHOAMI` != "root" ] ; then
$ECHO you must be root to run this script
exit
fi
#
# copy the DMC log file
#
cd $DMCLOGDIR
$CP $DMCLOG ${DMCLOG}.Day`$DATE -u +%d`
#
#
# truncate the DMC log file
#
$CAT - <
w
q
EOF
#
# put log entry in the syslog file
#
$LOGGER $DMCLOG truncated at `$DATE`
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 02:26 AM
тАО08-16-2010 02:26 AM
Re: How to truncate Log file to contain one month data only
#
#
# truncate the DMC log file
#
> $DMCLOG
rgs,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 02:33 AM
тАО08-16-2010 02:33 AM
Re: How to truncate Log file to contain one month data only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 02:47 AM
тАО08-16-2010 02:47 AM
Re: How to truncate Log file to contain one month data only
Sample scheduled script with cron:
# cd /var/spool/cron/crontabs
# cat root
30 1 1 * * /tmp/test.sh #Test
# ll root
-rw------- 1 root sys 30 Aug 16 12:43 root
The entries in a crontab file are lines of six fields each. The fields are separated by spaces or tabs. The lines have the following format:
minute hour monthday month weekday command
# crontab -l
rgs,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 04:03 AM
тАО08-16-2010 04:03 AM
Re: How to truncate Log file to contain one month data only
What is the format of the file and what are the answers to my other questions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 04:17 AM
тАО08-16-2010 04:17 AM
Re: How to truncate Log file to contain one month data only
Sample date string in the file is "Sat Aug 14 18:42:19 2010."
Yes, I need to keep exactly 1 month old data (number of days depends on the month as some months have 30 days, some have 31 and February have 29 or 28 days) in the file.
It doesn't matter when I run the script for as long as I will be able to truncate the file with 1 month old data kept.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 04:52 AM
тАО08-16-2010 04:52 AM
Re: How to truncate Log file to contain one month data only
#!/usr/bin/perl -w
use strict;
my @prev_months= qw(Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $fh;
open($fh,$ARGV[0]);
my $line;
my @parts;
while ($line = <$fh>) {
@parts=split(' +',$line);
if (($parts[1] eq $prev_months[$mon]) && ($parts[2] > $mday)) {
print $line;
}
if (($parts[1] eq $prev_months[($mon+1)]) && ($parts[2] <= $mday)) {
print $line;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-16-2010 04:55 AM
тАО08-16-2010 04:55 AM
Re: How to truncate Log file to contain one month data only
The number of days in a month is useless unless you are at the end of the month. If you are in the middle, what does it mean?
>It doesn't matter when I run the script for as long as I will be able to truncate the file with 1 month old data kept.
I assume we can make the assumption that if today is Aug 16, you want the info from July 16 to today?
You could make an awk script to scan every record for these fields:
Sat Aug 14 18:42:19 2010
if ($2 == prevmon && $3 >= currday && $5 >= prevyear ||
$2 == currmon && $5 == curryear) copy=1
You would have to do date arithmetic to figure out what the previous month's name and year is. The current month, day and year can be gotten from date(1).
Once "copy" is set, just read & print to the end of the file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2010 03:33 AM
тАО08-18-2010 03:33 AM
Re: How to truncate Log file to contain one month data only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2010 03:41 AM
тАО08-18-2010 03:41 AM