6

I tried to clear the content of error.log file on my apache server with commands

sudo >error.log

sudo truncate -s0 error.log

But in both cases I got -bash: error.log: Permission denied.

I tried the same thing while stopping apache, but still got the same message. After that I tried to clear it with sudo nano error.log and was able to remove everything.

So why was not I able to clear the log properly?

13

You should try this :

sudo sh -c ">/var/log/apache2/error.log"
  • Can you please explain what does this command do? – Salvador Dali Mar 18 '14 at 0:47
  • It runs an inline sh (shell) command under sudo context. Actually this inline sh command clears Apache error log (as you know) – krisFR Mar 18 '14 at 0:50
  • 1
    My guess is that > is not a command without an interpreter like sh or bash – krisFR Mar 18 '14 at 0:59
  • 1
    However your sudo truncate... command should work...That is the real thing i cannot explain by now – krisFR Mar 18 '14 at 1:07
  • 4
    Redirection operators are evaluated by your local shell before the sudo command even runs. sudo > foo opens the file foo for writing, then runs the command sudo . If you put it inside quotes or escape the redirection operator, then it is evaluated within the context of the sub-shell. – Zoredache Mar 18 '14 at 1:16

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.