Linux web server log rotation

Home > Search
  by

If you have a need to rotate the log files being used by a Linux web server, such as Apache, HTTPD, or Nginx, you certainly can use logrotate.conf. Or, you could create a how grown script, such as a Bash, Perl, or Python script. If you are going to be creating your own script, it's important to recognization that you will want to avoid using the mv (move) command when rotating the log files. If the mv command is used, the web server will continue to write records to the log files that was moved. For example, let's say you issue the following command, to rename the access_log. This is because the mv command simply renamed the log file, and the web server knows to continue to write events to the log file after it has been renamed.

~]# mv access_log access_log.old

 

After this command is issued, the web server will begin to write events to the access_log.old file. The web server would need to be restarted for the web server to start written events to a fresh access_log. On the other hand, when the cp (copy) command is used, the web server will continue to write events to the current access log. For example, let's say you issue the following command.

~]# cp access_log access_log.old

 

After issuing this command, you will have two log files, access_log and access_log.old. In this scenario, the web server will continue to write events to access_log, and will not write events to access_log.old. It usually makes sense to also erase all of the content of the log file after it has been copied, so that you are working with a fresh log file. Here are two different commands that could be used to wipe out the current log files.

~]# echo '' > access_log
~]# cp /dev/null access_log

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments