Search code examples
c++clinuxstderr

log rotation problem on custom application


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.


Solution

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