public
Last active

nginx log rotation script

  • Download Gist
nginx_rotatelogs.bash
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/bin/bash
 
LOGDIR=/usr/local/nginx/logs
PID=/usr/local/nginx/logs/nginx.pid
 
NOW=$(date +%Y%m%d-%H%M%S)
 
cd ${LOGDIR}
for i in *.log; do
mv ${i} ${i}.tmp.${NOW}
done
 
kill -USR1 $(cat ${PID})
sleep 1 #FIXME sleep time
 
if ls *.log >/dev/null 2>&1; then
for i in *.log.tmp.${NOW}; do
newfile=${i/.log.tmp.${NOW}/-${NOW}.log}
mv $i $newfile
bzip2 $newfile
done
fi

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.