Created
February 12, 2018 02:42
-
-
Save zyf0330/636d1899c6214862f048dcb6ef3072c2 to your computer and use it in GitHub Desktop.
logrotate for nginx log hourly
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/env bash | |
set -euo pipefail | |
function _d() { | |
echo `date +%Y%m%d-`$1 | |
} | |
cd /var/log/nginx | |
h=$((`date +%H`-1)) | |
if [ $h = -1 ]; then | |
h=23 | |
fi | |
d=`_d $h` | |
mv access.log access.log-$d | |
mv error.log error.log-$d | |
kill -USR1 `cat /var/run/nginx.pid` | |
sleep 1 | |
# not compress in busy time | |
if [[ $h = 18 || $h = 19 || $h = 20 ]]; then | |
echo 现在是忙时 | |
else | |
gzip -f access.log-$d | |
gzip -f error.log-$d | |
fi | |
if [ $h = 21 ]; then | |
gzip -f access.log-$(_d 18) | |
gzip -f error.log-$(_d 18) | |
gzip -f access.log-$(_d 19) | |
gzip -f error.log-$(_d 19) | |
gzip -f access.log-$(_d 20) | |
gzip -f error.log-$(_d 20) | |
fi | |
# clean old | |
n=240 | |
ls access.log-* -t | cut -d$'\n' -f $n- | xargs rm -f | |
ls error.log-* -t | cut -d$'\n' -f $n- | xargs rm -f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment