Skip to content

Instantly share code, notes, and snippets.

@pimentelra
Created August 22, 2019 16:16
Show Gist options
  • Save pimentelra/b3473c3e8dcecba35ccfe5664e35f59b to your computer and use it in GitHub Desktop.
Save pimentelra/b3473c3e8dcecba35ccfe5664e35f59b to your computer and use it in GitHub Desktop.
Bash script to compress and rotate nginx logs
#!/usr/bin/env bash
log_name='log_prefix'
for f in /var/log/nginx/${log_name}*.log; do
pattern="(.*)($log_name)(.*)"
[[ "$f" =~ $pattern ]]
/bin/gzip ${f} && /bin/kill -USR1 $(cat /run/nginx.pid) && /bin/mv ${f}.gz /var/log/nginx/$(date +%F-%T)${BASH_REMATCH[3]}.gz
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment