What is the output of this Raspberry Pi shell script that rotates a log file by renaming and compressing it?
#!/bin/bash LOGFILE="/var/log/myapp.log" if [ -f "$LOGFILE" ]; then mv "$LOGFILE" "$LOGFILE.$(date +%Y%m%d)" gzip "$LOGFILE.$(date +%Y%m%d)" echo "Log rotated" else echo "No log to rotate" fi
Assume the log file exists before running the script.
The script checks if the log file exists. If it does, it renames it with the current date and compresses it, then prints 'Log rotated'. If not, it prints 'No log to rotate'. Since the file exists, the output is 'Log rotated'.
Which find command will delete all compressed log files older than 7 days in /var/log?
Remember -mtime +7 means files modified more than 7 days ago.
Option A correctly finds files ending with .gz older than 7 days and deletes them. Option A deletes files newer than 7 days. Option A matches files exactly 7 days old. Option A misses the terminating \; for exec and will cause an error.
What error does this Python script raise when trying to rotate logs?
import os
import datetime
logfile = '/var/log/myapp.log'
if os.path.exists(logfile):
newname = logfile + datetime.datetime.now().strftime('%Y%m%d')
os.rename(logfile, newname)
os.system(f'gzip {newname}')
print('Rotated')
else:
print('No log')Consider typical permissions for /var/log on Raspberry Pi.
The script tries to rename and compress a log file in /var/log, which usually requires root permissions. Without proper permissions, it raises PermissionError.
Which option contains the syntax error in this Bash snippet?
for file in /var/log/*.gz
do
if [ $(stat -c %Y "$file") -lt $(date -d '7 days ago' +%s) ]; then
rm "$file"
fi
doneCheck the syntax of the for loop header in Bash.
In Bash, the for loop header must end with a semicolon or newline before 'do'. Here, the newline is present, so no semicolon is needed. However, if the code is all on one line, a semicolon is required. Since the snippet shows a newline, no syntax error exists in the for loop header. The single quotes in date command are correct. The if condition brackets are balanced. So the correct answer is no syntax error.
A Raspberry Pi rotates logs daily by renaming myapp.log to myapp.log.YYYYMMDD and compressing it. The cleanup script deletes compressed logs older than 10 days. If logs have been rotated daily for 15 days, how many compressed log files remain after running cleanup?
Think about which files are deleted and which remain based on age.
The cleanup deletes logs older than 10 days, so logs from days 1 to 5 (older than 10 days) are deleted. Logs from days 6 to 15 remain, totaling 10 files. But since the question asks how many remain after cleanup, the answer is 10.