How to compress and process log files automatically by Shell
This article mainly explains "Shell how to achieve automatic compression processing log files", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Shell how to achieve automatic compression processing log files" bar!
Shell handles logs and automatically retains logs for 30 days. If logs for more than 14 days are automatically compressed and stored in gz format, this can save disk space and back up logs regularly. I use log files as an example here, and other requirements can also be adjusted according to the content of the script.
#! / bin/sh# (c) Copyright 2012 Eric. All rights reserved.## Logs expired 30 days.## * cron# 1 6 * / bin/sh / var/app/shell/logs_arrange.sh > / var/app/shell/cron_logs_arrange.log#### # TODAY= `date +% s` # how many days ago will the logs to be zipPK_DAYS_AGO=14PK_DATEDIFF= `expr ${PK_DAYS_AGO}\ * 86400`PK _ EXT=gz# how many days ago will the logs to be removedDAYS_AGO=30DATEDIFF= `expr ${DAYS_AGO}\ * 86400`cleanDir () {directory=$1if [[- d ${directory}]] Thenfor file in `ls-1 ${directory} | grep-v '.log $' `; doctime= `stat-c% Y {directory} / ${file} `extname= `expr ${TODAY}-${ctime} `extname= `ls ${directory} / ${file} | awk-F'.'{printf $NF} '`if [[${extname}! = ${PK_EXT}]]; thenif [[- f ${directory} / ${file}]] Thenzipta= `expr ${timediff}-${PK_DATEDIFF} `if [[${zipta}-gt 0]]; thenecho "Package ${directory} / ${file}" gzip-f ${directory} / ${file} > ${directory} / ${file}. ${PK_EXT} fififiif [[- f ${directory} / ${file}]]; thendelta= `expr ${timediff}-${DATEDIFF} `if [[${delta}-gt 0]] Thenecho "Removing ${directory} / ${file}" rm-rf ${directory} / ${file} fifidonefi} cleanDir "/ var/app/logs/webapps/admin" cleanDir "/ var/app/logs/webapps/api" cleanDir "/ var/app/logs/webapps/pos" Thank you for reading. That's what Shell says about how to compress log files automatically. After the study of this article, I believe that we have a deeper understanding of how to achieve automatic compression of Shell to deal with log files, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!