Challenge - 5 Problems
Log Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this log rotation snippet?
Consider this bash snippet that rotates a log file by renaming it with a timestamp. What will be the output if the current date is 2024-06-15 and the log file exists?
Bash Scripting
LOGFILE="app.log" BACKUP="app.log.$(date +%F)" if [ -f "$LOGFILE" ]; then mv "$LOGFILE" "$BACKUP" echo "Rotated $LOGFILE to $BACKUP" else echo "No log file to rotate" fi
Attempts:
2 left
💡 Hint
Check how the date command is used inside the variable BACKUP.
✗ Incorrect
The script checks if the log file exists. If yes, it renames it by appending the current date in YYYY-MM-DD format. The echo confirms the rename with the expanded date.
🔧 Debug
intermediate2:00remaining
Why does this log rotation script fail to rotate logs?
This script is intended to rotate a log file by compressing the old log and creating a new empty log. Why does it fail to compress the old log?
Bash Scripting
LOGFILE="server.log" if [ -f "$LOGFILE" ]; then gzip $LOGFILE > $LOGFILE echo "Log rotated and compressed" else echo "No log to rotate" fi
Attempts:
2 left
💡 Hint
Think about what gzip does to the original file after compression.
✗ Incorrect
gzip compresses the file and deletes the original. The script then creates a new empty file with the original name. This works as intended, but if the script expects the original file to remain uncompressed, it will fail.
🚀 Application
advanced3:00remaining
Which script correctly rotates logs keeping only last 5 backups?
You want a bash script that renames the current log file by appending the date, compresses it, and deletes backups older than 5 days. Which script does this correctly?
Attempts:
2 left
💡 Hint
Check which file is compressed and how old backups are deleted.
✗ Incorrect
Option A moves the log file to a dated backup, compresses that backup, deletes compressed backups older than 5 days, and creates a new empty log file. It uses rm -f with find -exec which is correct and safe.
📝 Syntax
advanced2:00remaining
Which option has a syntax error in this log rotation script?
Identify the option that contains a syntax error preventing the script from running.
Bash Scripting
LOGFILE="access.log" BACKUP="access.log.$(date +%F)" if [ -f "$LOGFILE" ]; then mv "$LOGFILE" "$BACKUP" gzip "$BACKUP" touch "$LOGFILE" fi
Attempts:
2 left
💡 Hint
Check the syntax of the if statement carefully.
✗ Incorrect
Option D is missing the semicolon or newline before 'then' in the if statement, causing a syntax error.
🧠 Conceptual
expert1:30remaining
What is the main reason to compress rotated logs in automation?
In automated log rotation scripts, why is compressing old log files important?
Attempts:
2 left
💡 Hint
Think about storage and resource management.
✗ Incorrect
Compressing old logs saves disk space by reducing file size, which is critical for systems generating large logs over time.