0

I have a process which is continuously writing into a log file. I want to empty the contents of the log file without having to restart the process.

I have used various commands:

: > log.txt
echo "" > log.txt
truncate -s 0 log.txt
cat /dev/null > log.txt

They all truncate/empty the content of the log file but only temporarily. When a few minutes later process updates the content, log file size (and contents) goes back to (and newer content) what it originally was.

what might be going on and how I can permanently empty the log file contents ?

2
  • You could use >> to append the newer content to a file. Commented Aug 12, 2021 at 10:44
  • log file for the running process cannot be changed. I want to rotate the logs at mid-night every day without having to restart the process which cant auto-rotate the log files. So previous days log file is are archived and log file is emptied to be filled again until next mid-night. Commented Aug 12, 2021 at 11:37

1 Answer 1

1

For me a permanently empty log is done with simply link it to the black hole named: /dev/null
So try it (as root) by yourself with: rm log.txt && ln -sfv /dev/null log.txt

Thats not only for security reason.
On a Raspberry Pi or embedded devices it makes the Memory Card living much longer.

Unwanted history files i link in this way for security reason...

# ls -lah .*history
lrwxrwxrwx 1 root root 9 Aug  6  2020 .ash_history -> /dev/null
lrwxrwxrwx 1 root root 9 Jun 12  2020 .bash_history -> /dev/null

...so every session starts with an empty one.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry this is not what I want to achieve. I want to rotate the logs at mid-night every day without having to restart the process which cant auto-rotate the log files. So previous days log file is are archived and log file is emptied to be filled again until next mid-night.
@tanatan You really should look into the logrotate command and its man-page man7.org/linux/man-pages/man8/logrotate.8.html
thanks @LéaGris. Had a quick look but not sure how this will fix the issue I am facing "The commands truncate/empty the content of the log file but only until process adds more logs to the log file. When a few minutes later process updates the log file, log file size goes back to what it originally was. I am not sure how the old content comes back in the log file when I had just emptied is using one of the command."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.