Skip to content

Instantly share code, notes, and snippets.

@fengjing
Forked from holysugar/nginx_rotatelogs.bash
Created June 7, 2024 08:16
Show Gist options
  • Save fengjing/6c7500c92c0899d686bd19f91fb1274c to your computer and use it in GitHub Desktop.
Save fengjing/6c7500c92c0899d686bd19f91fb1274c to your computer and use it in GitHub Desktop.
nginx log rotation script
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment