Rotating a log file is necessary so that your log file will not be too big.
When log files gets too big, it has bitten me in the a** before. Server runs out of disk space
and server process fails. Yes it is an embarrassing situation for any server admin. Save yourself the embarrassment and spent 5 minutes implementing this simple rotate script:
cp /var/log/nginx/error.log /data/log/tmp_nginx_log/nginx_error_`date +"%Y_%m_%d"`.log; truncate -s0 /var/log/nginx/error.log; kill -USR1 $( cat /var/run/nginx.pid )
The code above will do the following:
- copy the current log file from /var/log/nginx/error.log to /data/log/tmp_nginx_log/
while copying, it will also rename the file by appending specific date to it. - next, it will truncate the current log file to zero size.
- finally, it will send message to nginx web server to reload gracefully and it will start logging errors into the new zero sized log file.
So now that we moved the log file to another directory what do we do with it there?
Since we named all files with their appropriate dates, we can easily review individual log files when we need too.
Another purpose for separating the log files into separate dates, is so that we can clean-up old log files we no longer want. For example if you want to only keep log files for 30 days, you can easily run the following command or schedule in your crontab.
find /data/log/tmp_nginx_log -type f -mtime +3 | xargs rm
The command above will remove any files older than 3 days from /data/log/tmp_nginx_log directory.
For more options you can do to clean up files, read my blog about how to Remove Files in Directory Recursively.
No comments:
Post a Comment