Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automatically empty Apache error log files UNIX
If, like me, you do a lot of Perl / HTML / Apache development on you desktop OS X system, you'll have noticed the error_log and access_log files getting massive and out of control. I normally have mine open in a web browser, so I can reload it to see what errors my code is producing. But at upwards of 10MB in size, it can slow down event the best of browsers. So, here is a little command you can set up as a cron job ever five mins or so to keep the error_log in check:
cd /private/var/log/httpd; tail error_log > error_log2; \
tail error_log2 > error_log
Make sure it's run as root. Now your error_log will be pruned every so often and won't slow you or your browser down.

[robg adds: The above command will overwrite your error_log file -- so make sure there's nothing in there you need to keep before you try this!]
    •    
  • Currently 2.67 / 5
  You rated: 3 / 5 (6 votes cast)
 
[10,299 views]  

Automatically empty Apache error log files | 7 comments | Create New Account
Click here to return to the 'Automatically empty Apache error log files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically empty Apache error log files
Authored by: nhajratw on Oct 04, '04 11:01:33AM

A good way to monitor log files so you don't have to keep reloading a web browser is to use the 'tail' command. From the Terminal, type:

tail -100f logfile_name

This will give you a realtime look into your log file.

Open multiple terminals for multiple log files.



[ Reply to This | # ]
Automatically empty Apache error log files
Authored by: TvE on Oct 04, '04 11:30:51AM

Just my comment - except you beat me to it :-)



[ Reply to This | # ]
Automatically empty Apache error log files
Authored by: mhagers on Oct 04, '04 11:53:41AM

What's wrong with the good old console app?
automatically does the tail command for you, with the added bonus that you can wipe the window clean to start afresh.
I always have it open whenever I'm writing php code.



[ Reply to This | # ]
Automatically empty Apache error log files
Authored by: bkazez on Oct 04, '04 01:43:41PM

There's nothing wrong with the Console except that it means needing to have one extra app open. It's simpler to use tail -f.

---
http://ben.kazez.com/



[ Reply to This | # ]
Automatically empty Apache error log files
Authored by: bkazez on Oct 04, '04 01:47:16PM

You can actually do this more elegantly without a cron job. Simply use the CustomLog directive in the Apache configuration file. One example is provided here:

http://httpd.apache.org/docs-2.0/programs/rotatelogs.html

Ben

---
http://ben.kazez.com/



[ Reply to This | # ]
Automatically empty Apache error log files
Authored by: GEllenburg on Oct 04, '04 04:33:51PM

You need to restart Apache after doing this hint.

Apache keeps the log-files open (at the file-descriptor level), so while you may have effectively overwritten the file, the disk space is continuing to be consumed because the inode that the file references is still locked opened by Apache.

Just append an "apachectl graceful" to the end of your command to restart apache.

Alternately, somebody else mentioned the rotatelogs script, which is a much cleaner and effective way of maintaining your access_log and error_log files.

---
He who does not challenge one\'s elected decisions today, may find himself challenged by them tomorrow.



[ Reply to This | # ]
Automatically empty Apache error log files
Authored by: cilly on Oct 04, '04 08:14:57PM
It is better to edit the weekly crontab located at:

/etc/periodic/weekly/500.weekly

I added the following section after the /var/log part:

cd /var/log/httpd
for i in access_log agent_log error_log referer_log ssl_engine_log ssl_request_log; do
if [ -f "${i}" ]; then
echo -n " $i"
if [ -x /usr/bin/gzip ]; then gzext=".gz"; else gzext=""; fi
if [ -f "${i}.3${gzext}" ]; then mv -f "${i}.3${gzext}" "${i}.4${gzext}"; fi
if [ -f "${i}.2${gzext}" ]; then mv -f "${i}.2${gzext}" "${i}.3${gzext}"; fi
if [ -f "${i}.1${gzext}" ]; then mv -f "${i}.1${gzext}" "${i}.2${gzext}"; fi
if [ -f "${i}.0${gzext}" ]; then mv -f "${i}.0${gzext}" "${i}.1${gzext}"; fi
if [ -f "${i}" ]; then mv -f "${i}" "${i}.0" && if [ -x /usr/bin/gzip ]; then gzip -9 "${i}.0"; fi; fi touch "${i}" && chmod 640 "${i}" && chown root:admin "${i}" fi
done

---
cilly @ http://www.cilly.dyndns.org/

[ Reply to This | # ]