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

$ 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.

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 ?

If you mean clear the whole content and remove all lines inside the file then :

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.

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:

mv Big_File Save_Big_File

If you do not want to save:

tail Big_File > Big_File

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.

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.

Yes,Author seems to be a UNIX GURU. This is a small part of a Commerical Application.

So you meant to say." I cant do anything other than watch the log growing in size !!"

The problems are

(a) if the program always keeps the file open, eg it does not reopen on SIGHUP or every message or day.

(b) trying to clear it atomically

Two parts to my solution...

  1. There is a UNIX API called "truncate" which will set a file to a certain number of bytes.

  2. we can suspend and resume the program with signals SIGSTOP and SIGCONT

So the steps are

  1. write a tiny C program that wraps up truncate
#include<unistd.h>
#include <errno.h>
int main(int argc,char **argv) {
int rc=0;
      while (--argc)
      {
            const char *p=*++argv;
            if (truncate(p,0UL)) {
                 perror(p);
                 rc=1;
            }
      }
      return rc;
}
  1. do the following

kill -STOP pid-of-process
cp logfilename backup
truncate logfilename
kill -CONT pid-of-process

NO. I meant to say:

Edit the config file, change the log file to another different name.
Send SIGHUP to the process when you are logged in as root.
truncate the old big file.

Try doing
>|log_file