Linux Basic — Introduction to Automation Practice

less than 1 minute read

Practice Exercises — Introduction to Automation

Build automation-ready scripts following the patterns from this chapter.


Warm-up

  1. Write a script that logs “hello” with a timestamp to /tmp/automation.log. Make it non-interactive.
  2. Add a lock file to prevent concurrent runs.
  3. Test that the lock works by running two instances simultaneously.

Backup

  1. Implement a simple backup script that tarballs /home/user/docs to /backup/docs-YYYY-MM-DD.tar.gz.
  2. Add retention: delete backups older than 7 days.
  3. Add logging and a failure alert (echo to a file simulating email).

Log Cleanup

  1. Write a script that finds and deletes .log files in /var/log/myapp older than 14 days.
  2. Compress .log files older than 1 day but not yet gzipped.
  3. Run a dry-run with -print before using -delete.

Scheduling

  1. 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 pipefail and trap for 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

Updated: