Quote:
Originally Posted by
Corona688
Even proper logging will misbehave this way if you blithely delete the file while it's in use. It's a very common beginner administration mistake. Just truncate the file instead of deleting it.
Depending on the flags used when the file was opened, where the file is written to by the writing process, and the specifics of the underlying file system, even truncating an open file isn't guaranteed to work.
And since redirected stdout and stderr tend to be done with ">>" pipe operators, the log files get opened with the O_APPEND flag with all writes going to the current file descriptor offset for the running process, truncations of such files rarely work. At best if the file system supports sparse files you will release the space by turning the log file into a sparse file.
Redirecting stdout and/or stderr for log files is a bad idea. The running process is tied to the log file(s) and the log file(s) are tied to the running process. You can't "age" the log files. You can't delete them. You can't even safely truncate them. Not only that,. using redirected stdout/stderr also causes all kinds of problems with log entry atomicity for multithreaded applications.
If logging is important, use a proper logging system designed for logging. Operating systems come with those.