How to empty a personal log file?

i'm running a program and it should keep running 24/7, and ofc i'm saving its outputs in a log file
"programName > logfile"
the thing is, when i try to delete its content and not the file itself since the program is using it. i can't find a way to clean it up, i used:

cat /dev/null > "logfile"
echo "" > "logfile"
> log file
:> logfile
trancate "logfile" -s 0

none of them works, i know about the logrotate but i'm trying to avoid downloading or using any other package beside the one already exist.
i think, the program is writing its output in a buffer and then write it back in the file, so i need to empty that buffer first.
hope you can help me with this.

If there's nothing important, you can try to disable logging, or write the log directly to /dev/null, or symlink it to /dev/null.
You can also try sending SIGHUP or other signals to the process, which may help to reload the log or reset the buffer.

This what Apache does (or did, in the good old days):

  • Rename the log file externally (the program will continue to use the renamed file, because it is already open).
  • Send a signal to the running program.
  • When the program receives the signal, it just closes and opens again the log file.
1 Like