Operating System - HP-UX
1753479 Members
4810 Online
108794 Solutions
New Discussion юеВ

Re: How to trim log file with out restarting syslogd

 
Raj D.
Honored Contributor

Re: How to trim log file with out restarting syslogd

F=user_log;k=200;t=`sed -n "$=" $F`;d=`expr $t - $k` ; perl -i -ne "print unless 1 .. $d" $F #Hth
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: How to trim log file with out restarting syslogd

Senthil,


> I would like to trim to 1or 2MB ..



In the above example: (Details given ..)


F = Filename to trim = user_log
You may be want to keep last few lines of the log files , say last 200 lines you want to keep.

Keep = k = 200

Total_lines = t (Automatically calculated.)

Delete/Trim Lines = d = (Automatically calculated.)



- So here is the command you need to run to trim the file to just 200 line , or any minimum number you desire:



# cd /var/adm/syslog/
# F=user_log ; k=200 ; t=`sed -n "$=" $F`; d=`expr $t - $k` ; perl -i -ne "print unless 1 .. $d" $F

# wc -l user_log

200 user_log




- Now to see the size , you can use :
- # ls -l user_log #It should have trimmed to a small size in KB ).




Enjoy , Have fun!,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: How to trim log file with out restarting syslogd

F=user_log;k=200;t=`sed -n "$=" $F`;d=`expr $t-$k`;perl -i -ne "print unless 1 .. $d" $F
" If u think u can , If u think u cannot , - You are always Right . "
James R. Ferguson
Acclaimed Contributor

Re: How to trim log file with out restarting syslogd

HI (again):

@ Raj:

Using 'perl -ni -e...' to perform an inplace file update will *NOT* work for trimming files where a (daemon) process is required to continue to update the trimmed file without a restart. Perl performs this slight-of-hand by renaming the original input file and thus changing its inode. Thus, the process continues to write to the *old* file, and any logging to the new file doesn't occur.

Regards!

...JRF...
Raj D.
Honored Contributor

Re: How to trim log file with out restarting syslogd


> Perl performs this slight-of-hand by renaming the original input file and thus changing its inode.

- This is really true.

Thanks for correcting. Yes, perl changes the inode of the file ,so log update doesn't happen further to that file, unless the daemon is restarted again. James, Thanks again for the valuable input.
" If u think u can , If u think u cannot , - You are always Right . "
Suraj K Sankari
Honored Contributor

Re: How to trim log file with out restarting syslogd

Hi Senthil,
What i use to do is copy this file with time stamp and trim this file to 0 bytes
like
#cp -p user_log user_log.24Jun2010
#>user_log

Suraj