How do erase the contents of a error.log file but keep the file intact
46,610
Solution 1
You can use this:
>error.log
(typed just like that - an empty output redirection)
or
truncate -s0 error.log
Solution 2
You'll confuse the daemon. Erase the file then send SIGHUP
to nginx.
Related videos on Youtube
Author by
user27449
Updated on September 17, 2022Comments
-
user27449 almost 2 years
I want to erase the contents of the file error.log (nginx error log file), but I don't want to actually delete the file.
is this possible?
running ubuntu
-
Ignacio Vazquez-Abrams over 13 yearsIf you wanted to, sure. But that would disrupt service.
-
Dennis Williamson over 13 years@Ignacio: If the file already exists, either will truncate it. If the file doesn't exist, either will create it.
-
Ignacio Vazquez-Abrams over 13 yearsYou're right, my bad.
-
Dennis Williamson over 13 yearsI think both a null redirection and a
truncate
are atomic, so as long as no seeking is going on (especially if the only operation that's occurring is appending) it shouldn't be disruptive. -
davidkonrad about 10 yearsThe last option worked for me, for some reason sudo >error.log didnt work for me (ubuntu 12.04)
-
davidkonrad about 10 yearsOK, didnt knew that. Thank you for clarification!
-
divHelper11 over 5 yearsThe first one worked for me perfectly! Even though I dont understand what it does :D Thank you
-
Dennis Williamson over 5 years@divHelper11: In my answer I say it's an "empty output redirection". That means that "nothing" is being sent to the named file and that overwrites any existing contents. It's similar to
echo "some words" > output.txt
but nothing is going in.