Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Why does Apache refuse to write to log files (the ErrorLog/CustomLog ones) after I have manually deleted their contents?

It will not write to those log files again until after I restart Apache.

Why is this the case? How can I safely purge a log file without having to restart Apache?

I have Apache 2.2.14 on Ubuntu 10.04.

share|improve this question
    
How do you delete the contents ? –  Dom Oct 22 '12 at 14:01
    
@Dom I just open it with vim, do "dG" (which deletes all of the contents), and then save with "!wq". The ownership/group/permissions are not changed. –  AtomicFault Oct 22 '12 at 14:04
3  
@AtomicFault Prrrobably because that's not how you're supposed to rotate Apache logs. You should be using something like logrotate which sends an appropriate reload/restart signal to Apache(See Pedro's answer below). Nickgrim covered the "why" behind your logs stopping -- Apache is still writing to the old inode (which is no longer connected to the filesystem anywhere you can get at it) –  voretaq7 Oct 22 '12 at 15:59
add comment

2 Answers

up vote 11 down vote accepted

I just did a brief test:

$ echo vim test > vimtest
$ ls -i vimtest
35149 vimtest
$ vim vimtest
<dG, :wq>
$ ls -i vimtest
35148 vimtest

Note that vimtest has a different inode-number after editing it, and is thus actually a different file (albeit with the same name as the old file).

So, when you edit the file with vim it deletes the old file, and creates a new one with the same name. The problem you're seeing is caused by Apache still writing to the old (deleted) file (you can check this with lsof).

If you really want to truncate a log file, consider truncate -s 0 /path/to/file.log (which seems to truncate in-place)

share|improve this answer
2  
echo > /path/to/file.log also works –  Eric DANNIELOU Oct 22 '12 at 14:29
add comment

I would recommend forcing the rotation of the Apache2 log files with:

$ sudo logrotate -f /etc/logrotate.d/apache2

If you look into /etc/logrotate.d/apache2, you'll see that the Apache2 configuration has to be reloaded after deleting its log file with:

$ sudo /etc/init.d/apache2 reload

On Ubuntu you can alternatively do:

$ sudo service apache2 reload
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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