We have an apache server running on linux writing to a log file that is getting really large (access_log). Our server will begin running out of space. Is there a way to delete or truncate the file without restarting the server (we don't want any down time).
How to reset your log files
Sooner or later, you'll want to reset your log files (
error_log) because they are too big, or full of old information you don't need.
access_log typically grows by 1Mb for each 10,000 requests.
Most people's first attempt at replacing the logfile is to just move the logfile or remove the logfile. This doesn't work.
Apache will continue writing to the logfile at the same offset as before the logfile moved. This results in a new logfile being created which is just as big as the old one, but it now contains thousands (or millions) of null characters.
The correct procedure is to move the logfile, then signal Apache to tell it to reopen the logfiles.
Apache is signaled using the SIGHUP (-1) signal. e.g.
mv access_log access_log.old kill -1 `cat httpd.pid`
httpd.pid is a file containing the process id of the Apache httpd daemon, Apache saves this in the same directory as the log files.
Many people use this method to replace (and backup) their logfiles on a nightly or weekly basis.
log rotation is the long-term solution but the answer to your immediate question is to truncate the file something like this:
sudo cat /dev/null > /var/log/httpd/access_log
i'm assuming you're not logged in as root and assuming the location of your log file but you should be able to adjust the command as needed and quickly truncate an open log file w/o touching your running apache processes.
- it is a powerful tool which gives configurable options for rotating logs.
- it also has facility to run command during
copytruncateenables you to copy existing files and then truncate it. The copy can be moved to another storage such as hadoop, s3 for backup if desired
- Moreover a cron can be set such as
/usr/sbin/logrotate --force /etc/logrotate.hourly.conf 2>&1 >> /tmp/loggerby using
For more info
Had the same problem with a process and fixed it using Linux named pipe. Here is what I did (assuming /tmp/job.log is the log file):
- Stop the job
- rm /tmp/job.log
- created a named pipe using mkfifo: mkfifo /tmp/job.log
- ran: cat /tmp/job.log | gzip > /tmp/job.log.gz
- Start the job
This way, I was able to keep the log and reduce the disk usage drastically
you can replace gzip with any command that filters, rotates, ...