tanatan
tanatan

Reputation: 41

How to empty the content of a log file

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 ?

Upvotes: 0

Views: 2568

Answers (1)

koyaanisqatsi
koyaanisqatsi

Reputation: 2793

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.

Upvotes: 1

Related Questions