skip to Main Content

I have server program, written in C++. I usually start it as

db_net 2> log_file

I do not use anything special about logging, just print on stderr.

However sometimes, the log_file become huge. If I do

echo > log_file

It does not have effect, the file is not shrieked to zero. I know this worked 10 years ago, but now it does not.

Question is following:

  • Can this be fixed from OS side somehow (I am running on Linux), without restarting the application
  • if it can not be fixed from the OS, can I fix this from the application? I can close and open stderr, but I do not really know the filename where stderr points to, since it is redirected. C solution is OK too.

I know MySQL doing this with flush logs; and apache / nginx can do the same, however they know the filename and in their case is easy to reopen.

I know I can implement log file inside configuration, but currently I am looking for fast solution.

Filesystem is XFS.

2

Answers


  1. Truncating the file by itself does not change the file offset of your stderr file descriptor.

    From the application you can call:

    ftruncate(2, 0);
    lseek(2, 0, SEEK_SET);
    
    Login or Signup to reply.
  2. Use append mode:

    db_net 2>> log_file
    

    Now when you manually truncate the file, the process will just continue to append to the new, empty file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search