Last active
June 23, 2017 04:55
-
-
Save aimtiaz11/4efb0cf0b190805775f2 to your computer and use it in GitHub Desktop.
UNIX - A simple script to rotate log files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## The following is a simple script to rotate log files. Schedule it using crontab to regularly | |
| ## rotate log files. | |
| ## The script works by backing up the target log file (using cp) and then emptying it out. | |
| ## Usage: | |
| ## ./rotatelog.sh <<file name>> | |
| ## License MIT | |
| DATESTAMP=`date +'%Y%m%d-%H%M'` | |
| BACKUP_LOG_FILE=backup.$DATESTAMP.log | |
| # Is 1st argument passed? | |
| if [[ -z $1 ]] | |
| then | |
| echo "First argument must be the file path" | |
| exit 0 | |
| else | |
| LOG_FILE=$1 | |
| fi | |
| ## Is the 1st argument a valid file path? | |
| if [[ -e $LOG_FILE ]] | |
| then | |
| echo "File exist" | |
| echo "Backing up....." | |
| else | |
| echo "File does not exist" | |
| exit 0 | |
| fi | |
| cp $LOG_FILE $BACKUP_LOG_FILE | |
| cat /dev/null > $LOG_FILE | |
| echo "Backup Complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment