10

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.

3
  • How do you delete the contents ?
    – Dom
    Oct 22, 2012 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. Oct 22, 2012 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, 2012 at 15:59

2 Answers 2

14

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)

1
  • 2
    echo > /path/to/file.log also works
    – user130370
    Oct 22, 2012 at 14:29
8

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .