Created
October 23, 2018 08:02
-
-
Save mqu/b992abe6a9b9ca76614d74d6d219a9d1 to your computer and use it in GitHub Desktop.
rotating log file with bash script
This file contains 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
#!/bin/bash | |
# rotate log files with bash script. | |
function _logrotate { | |
file=$1 | |
MAXLOG=10 | |
# return if $file does not exists | |
[ ! -f $file ] && return 0 | |
# process file rotation | |
for i in `seq $((MAXLOG-1)) -1 1`; do | |
[ -f "${file}.${i}" ] && mv -fv "${file}."{$i,$((i+1))} | |
done | |
mv $file $file.1 | |
return 0 | |
} | |
# sample usage for demo | |
file=log/test.log | |
_logrotate $file | |
date >> $file | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment