# Created on savesnippets.com ยท https://savesnippets.com/SVWjFFcMXgH1Md LOG="/var/log/myapp/app.log" MAX_AGE_DAYS=14 # Rotate if larger than 50 MB if [[ -f "$LOG" && $(stat -c%s "$LOG") -gt 52428800 ]]; then STAMP="$(date -u +%Y%m%d-%H%M%S)" mv "$LOG" "$LOG.$STAMP" gzip "$LOG.$STAMP" # background it if log is huge # Tell the app to reopen if needed pkill -USR1 -f myapp || true # SIGUSR1 = "reopen logs" by convention fi # Clean up archives older than 14 days find "$(dirname "$LOG")" -name "*.gz" -type f -mtime +$MAX_AGE_DAYS -delete # Or just use logrotate (the right answer for anything long-term): # /etc/logrotate.d/myapp: # /var/log/myapp/*.log { # daily; rotate 14; compress; delaycompress; missingok; notifempty; # postrotate; systemctl reload myapp; endscript # }