1834769 Members
3040 Online
110070 Solutions
New Discussion

Re: Trim Active Log

 
SOLVED
Go to solution
susee_sundar
Regular Advisor

Trim Active Log

Hi ,

How can I trim my active log file ..
18 REPLIES 18
Pete Randall
Outstanding Contributor
Solution

Re: Trim Active Log

cat /dev/null > my_active_log_file


Pete

Pete
Torsten.
Acclaimed Contributor

Re: Trim Active Log

There are a lot of different logs.


A good way is to use the "guided" function with SAM.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Matthew Ghofrani
Regular Advisor

Re: Trim Active Log

Hi;
I have a nice script that trims/archives syslog.log while managing syslogd. If interested, respond and I will send you the complete script. You could modify it for other log files.

Matthew from Boston
Life is full of bugs
Mridul Shrivastava
Honored Contributor

Re: Trim Active Log

The preferred way is to trim files using sam,
# sam
Routine Tasks --> System Log Files
then select the log file to trim.

else following command can be used:

cat /dev/null >

Time has a wonderful way of weeding out the trivial
Bill Hassell
Honored Contributor

Re: Trim Active Log

The simplest method is to use sam as it understands most of the common logfiles. However, the manual method is:

cd /logfile_directory
cp logfile logfile.old
cat /dev/null > logfile

Do not rename the file or create a new one since the file may be in use by a program. The above method allows for a program to continue adding to the logfile.

Logfile trimming is a regular task and will have to be repeated. Of course, the logfiles should be examined for any problems.


Bill Hassell, sysadmin
Nguyen Anh Tien
Honored Contributor

Re: Trim Active Log

1, backup your current log file for later refer
#cp /path/your_log_file /path/backup_file
2, Trim
#>/path/your_log_file
HTH
HP is simple
susee_sundar
Regular Advisor

Re: Trim Active Log

Hi I got a good idea in trimming the log file.

But few doupts.

#/dev/null > active_log_file

What the command is doing.?
How it is trimming the log file.?

As per my Knowledge it is sending the content of active log to the null port ..If so.. Then how the Log file is get trimmed..

I got confused .. Anybody here to clear this confusion..
Arunvijai_4
Honored Contributor

Re: Trim Active Log

Hi Sundar,

But few doupts.

#/dev/null > active_log_file

What the command is doing.?
This command nullifies the active_log_file.
How it is trimming the log file.?

As per my Knowledge it is sending the content of active log to the null port ..If so.. Then how the Log file is get trimmed..

Take a look at # man null

NAME
null - null file

DESCRIPTION
Data written on a null special file is discarded.

Reads from a null special file always return 0 bytes.

EXAMPLES
To create a zero-length file, use either of the following:

cat /dev/null > file
cp /dev/null file

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
susee_sundar
Regular Advisor

Re: Trim Active Log


#/dev/null > active_log_file

What the command is doing.?
This command nullifies the active_log_file.

SUCH THAT THE ACTIVE LOG FILE CONTENT WILL BE ERASED IS IT SO...



Arunvijai_4
Honored Contributor

Re: Trim Active Log

Yes, Thats correct. # >file is efficient way of doing.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
inventsekar_1
Respected Contributor

Re: Trim Active Log

i replied this around yesterday night, but something wrong... and i am repling again.
==========================================
i have some questions about /dev/null:

from man null:
Data written on a null special file is discarded.
Reads from a null special file always return 0 bytes.

To create a zero-length file, use either of the following:

cat /dev/null > file
cp /dev/null file

#ll null
crw-rw-rw- 1 bin bin 3 0x000002 Jun 12 01:15 null
#wc null
0 0 0 null

1.)i thought that /dev/null is a exe like rm or cat, but its a charactor special file.
anyhow its size also, zero. then how it is working?
2.) cat /dev/null > my_active_log_file
this command will delete the content of that "my_active_log_file". if so,
#rm my_active_log_file
#touch my_active_log_file also do the same function..right?

3). thru sam, we can do:
1. trim to zero, 2. To the Recommended size, 3. To a Line Number 4. To a Percentage,

how to do this four things thru commands?
ie how to trim a file to its 50% of original size?


Be Tomorrow, Today.
Devender Khatana
Honored Contributor

Re: Trim Active Log

Hi,

Sekar FYI,

#cat /dev/null >file
or
#>file

Will do the same thing.

Also it is the efficient way of trimming the log files. Allthough you can do it by other ways as well like removing and recreating files. But it will also involve two more steps of changing ownerships and permissions.
Also if some process tries to access the same while while you have removed it, it may experience some error.

So why to follow these many steps when you can do it with one simple commands itself.

HTH,
Devender
Impossible itself mentions "I m possible"
susee_sundar
Regular Advisor

Re: Trim Active Log

Hi,

I think That I got something But not sure.,
From the above reply that i find that

We can take the log file backup by using cp

#cp log_file log_file_old

then we can forwrd it to the null port such that

#/dev/null > log_file

such that the log_file become empty..

Is it right..

1. If so then what will happen to the new logs.. ?

2. And how the log file start generating logs again.. ?
Nguyen Anh Tien
Honored Contributor

Re: Trim Active Log

Hi Susee.
#/dev/null > log_file
and
# > log_file
both will re-fresh your logfile with no data inside (make the log file empty) but keep the time-stamp of that file ( day of creation, owner, permission)
By do-ing as this way. Your application can continue to put log to log-file as usual.
HTH
HP is simple
susee_sundar
Regular Advisor

Re: Trim Active Log

Hi ,

Its Amazing Response and i Got The solution from the last reply..

Thanks to All..

I am closing the Thread.
Sandman!
Honored Contributor

Re: Trim Active Log

Sundar,

There are quite a few ways to trim a file under HP-UX as in...

# > active_log_file
or
# cat /dev/null > active_log_file
or
# cp /dev/null active_log_file
or
SAM

...SAM is able to manipulate the file size in more ways because behind the scenes it calls the std C lib APIs like truncate and ftruncate.

You can develop your own C program(s) utilizing those lib routines and wrap them in a menu driven shell script which essentially provides the same functionality as SAM does.

~hope it helps
Arturo Galbiati
Esteemed Contributor

Re: Trim Active Log

Bill Hassell
Honored Contributor

Re: Trim Active Log

Very important:

/dev/null can be used to write or read. When it is used as a source file, it is an empty file. When you simply cat the null file and redirect it into another file, the destination file is reduced to zero length. This is *NOT* the same as:

rm logfile
touch logfile

Several mistakes have occurred using rm. First, the logfile has very specific permissions and removing the file means that these permissions and ownership must be restored for proper operation. Now if the logfile is still open, then the rm does not work correctly and the touch simply updates the timestamp. DON'T use rm.

Trimming a logfile to 50% requires a script to calculate the number of records, read that many records from the start into the second logfile. Then using tail (it has limitations on size) or awk, read the last half into a temp file, then cp the tempfile on top of the current file and delete the tempfile. Not easy at all. It is much simpler to copy the current file then zero the current file.


Bill Hassell, sysadmin