Linux Basic — Introduction to Automation Practice
Practice Exercises — Introduction to Automation
Build automation-ready scripts following the patterns from this chapter.
Warm-up
- Write a script that logs “hello” with a timestamp to
/tmp/automation.log. Make it non-interactive. - Add a lock file to prevent concurrent runs.
- Test that the lock works by running two instances simultaneously.
Backup
- Implement a simple backup script that tarballs
/home/user/docsto/backup/docs-YYYY-MM-DD.tar.gz. - Add retention: delete backups older than 7 days.
- Add logging and a failure alert (echo to a file simulating email).
Log Cleanup
- Write a script that finds and deletes
.logfiles in/var/log/myappolder than 14 days. - Compress
.logfiles older than 1 day but not yet gzipped. - Run a dry-run with
-printbefore using-delete.
Scheduling
- Schedule your backup script to run daily at 2 AM using cron. Verify it runs by checking the log.
Verification Checklist
- All scripts use
set -euo pipefailandtrapfor cleanup - Lock files prevent overlapping runs
- Scripts log start, progress, and completion
- Absolute paths are used for commands and files
- Dry-run mode tested before destructive operations
Next chapter → Scheduling with Cron