Write a script to rotate a log file manually: compress the current one and truncate it without restarting the app.

IntermediateHands-onBash scripting tasks
Answer
#!/usr/bin/env bash
set -euo pipefail
LOG=/var/log/app/app.log
TS=$(date +%F-%H%M)
cp "$LOG" "$LOG.$TS"
: > "$LOG"            # truncate in place, same inode
gzip "$LOG.$TS"
find /var/log/app -name 'app.log.*.gz' -mtime +30 -delete

Key trick → mv would leave the app writing to the old inode. Copy-then-truncate (logrotate's copytruncate) keeps the same file handle valid, at the cost of possibly losing a few lines written between the copy and the truncate.

All Real-World Interview questions Roadmap: Interview readiness

More on Bash scripting tasks