A colleague's script deletes files older than 30 days from `/var/log/app`. You are asked to review it before it goes into cron. What do you look for?
IntermediateScenarioAutomation scripting & CLI design
Answer
Situation read → Destructive script, unattended execution. Review for blast radius and safety.
What I'd do →
- Check the path is a constant or validated, never empty: a bug that turns the base into
/or""must abort. - Check it uses
Path.rglobwith explicit patterns (*.log*) and skips symlinks (p.is_symlink()), so it cannot follow a link out of the directory. - Confirm age uses
st_mtimeagainst timezone-consistenttime.time(). - Require a
--dry-rundefault or flag, log every deletion, and exit non-zero on errors. - Catch
PermissionErrorper file and continue, instead of aborting halfway. - Ask whether
logrotatealready does this, which is usually the better answer.
Why → Cleanup scripts fail dangerously at their edges: empty variables, symlinks and permissions.
Interview tip → Mention logrotate or find -mtime +30 -delete; not every problem needs Python.
More on Automation scripting & CLI design
Your team has a 300-line Bash script that parses JSON with `jq`, loops over AWS accounts and retries with `sleep`. It breaks every month. Do you rewrite it in Python?BeginnerYou need a script that ops engineers run from laptops and that CI runs nightly. How do you design its interface?IntermediateA script reads a 15 GB access log to count 5xx errors per endpoint, and the box runs out of memory. How do you fix it?BeginnerYou must run the same Python automation on 40 servers that have Python 3.6, 3.9 and 3.12. What do you do?IntermediateA cron job that runs a Python script fails silently. Nobody noticed for two weeks. How do you make sure that never happens again?IntermediateProduct wants a Slack notification whenever a deploy finishes. You have 30 minutes. What do you build?Beginner