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.

Share:
46,610

Related videos on Youtube

user27449
Author by

user27449

Updated on September 17, 2022

Comments

  • user27449
    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
    Ignacio Vazquez-Abrams over 13 years
    If you wanted to, sure. But that would disrupt service.
  • Dennis Williamson
    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
    Ignacio Vazquez-Abrams over 13 years
    You're right, my bad.
  • Dennis Williamson
    Dennis Williamson over 13 years
    I 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
    davidkonrad about 10 years
    The last option worked for me, for some reason sudo >error.log didnt work for me (ubuntu 12.04)
  • davidkonrad
    davidkonrad about 10 years
    OK, didnt knew that. Thank you for clarification!
  • divHelper11
    divHelper11 over 5 years
    The first one worked for me perfectly! Even though I dont understand what it does :D Thank you
  • Dennis Williamson
    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.