Skip to content

Instantly share code, notes, and snippets.

@zyf0330
Created February 12, 2018 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zyf0330/636d1899c6214862f048dcb6ef3072c2 to your computer and use it in GitHub Desktop.
Save zyf0330/636d1899c6214862f048dcb6ef3072c2 to your computer and use it in GitHub Desktop.
logrotate for nginx log hourly
#!/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