Clear log file in use. | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Clear log file in use.

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-19-2007
coolbhai coolbhai is offline
Users Awaiting Email Confirmation
 
Join Date: Jun 2007
Last Activity: 5 January 2011, 2:42 AM EST
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Clear log file in use.

Hai,
I have a program which updates the result in a log file, as the program runs 24*7,the size of log file keeps on increasing.
Can you help me with a shell command which will clear the content of a log file in use.

1) I tried tail -10 logfile > logfile ( the content is not changed )
2) > logfile ( no change in filesize )
3) echo -n >logfile ( no change in filesize )

Please advice.

Thanks in advance
Coolbhai
Sponsored Links
    #2  
Old 06-19-2007
kamitsin's Avatar
kamitsin kamitsin is offline
Users Awaiting Email Confirmation
 
Join Date: Nov 2006
Last Activity: 19 June 2012, 2:15 AM EDT
Location: /dev/null
Posts: 178
Thanks: 0
Thanked 1 Time in 1 Post

Code:
$ touch logfile

This should serve your purpose if you just want to clear the logs else you can always write a shell script to rotate the logs.
Sponsored Links
    #3  
Old 06-19-2007
coolbhai coolbhai is offline
Users Awaiting Email Confirmation
 
Join Date: Jun 2007
Last Activity: 5 January 2011, 2:42 AM EST
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry,
Even the touch command didnt serve my purpose;

The file I want to clear is always in use. Some program is appending it content always (even for each microseconds).

Any suggestions ?
    #4  
Old 06-19-2007
qfwfq qfwfq is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 12 February 2010, 2:54 PM EST
Location: Canada
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
If you mean clear the whole content and remove all lines inside the file then :


Code:
cat /dev/null > your_file

Make sure you know what you are doing because that really clears the content. The touch command just updates the stamped time of the file.
Sponsored Links
    #5  
Old 06-19-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts
Coolbhai,
If the way your application was designed, it is appending to the file
each microsecond, you must take the risk of loosing some data whenever
you refresh the log file.
If you are appending, then if the file does not exist, the append
will create it.
Here is one possible solution if you want to save the file:

Code:
mv Big_File Save_Big_File

If you do not want to save:

Code:
tail Big_File > Big_File

Sponsored Links
    #6  
Old 06-19-2007
coolbhai coolbhai is offline
Users Awaiting Email Confirmation
 
Join Date: Jun 2007
Last Activity: 5 January 2011, 2:42 AM EST
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry Again
I tried
$ tail logfile > logfile

result : no change in file size ,for a fraction of a second,file size droped to zero but again regained its original size.

I have a wierd suggestions:
I know the 'fuser' of that particluar file, is there any way by which that I transfer 'fuser' to temp-file and clear the logfile and bring the fuser back to actual logfile.

NB : Even if I rename the file,the renamed file keeps on updating.So process Id has some role in this process.

Please advice.
Sponsored Links
    #7  
Old 06-19-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 21 October 2014, 9:59 PM EDT
Location: NM
Posts: 10,246
Thanks: 281
Thanked 803 Times in 747 Posts
Assuming that the author of the code writing the never-ending file knows even a little bit about unix:

1. For this process there is a configuration file, which names the output logfile.
2. The process "reconfigures" itself when it receives a SIGHUP signal,
by re-reading the config file, and if the name of the logfile changed, then close the old one, open the new one.

This is pretty much UNIX standard for a never-ending log writer program.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
clear extra spaces and tabs in a file goofist Shell Programming and Scripting 3 09-15-2010 04:22 AM
How to clear a file shira Shell Programming and Scripting 2 01-27-2009 10:13 AM
clear a file in PERL chriss_58 Shell Programming and Scripting 1 06-06-2008 04:57 AM
SED/AWK Script to clear log file using timestamp? davesimm Linux 2 03-03-2008 06:18 AM
How can I clear a line of a file? Nene Shell Programming and Scripting 1 08-12-2006 12:03 PM



All times are GMT -4. The time now is 05:37 AM.