Loading...

Lab 43: Scheduled Task Intervals

Configure system-wide recurring tasks using the correct cron interval directories and enforce executable permissions. Validate placement by enumerating the cron directories and confirming each script’s purpose and run frequency.

services core troubleshooting

Scenario

Your team needs you to configure system-wide recurring tasks that run at predictable intervals. You must place scripts in the correct cron interval directories so the system scheduler runs them automatically, and you must ensure scripts are executable so they actually fire.

Operator context

This is baseline automation work. Correct placement and executable permissions are what separate a working schedule from a silent failure.

Objective

  • Identify the system-wide cron interval directories.
  • Create an hourly maintenance script.
  • Create a daily backup script.
  • Create a monthly archive sync script.
  • Verify script placement and executable permissions.

Concepts

  • Interval-based scheduling using /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly.
  • System-wide drop-in cron directories versus user crontabs.
  • Executable permissions as a requirement for cron scripts.
  • Using small maintenance scripts to automate routine tasks.

Walkthrough

Step 1 : Inspect the cron interval directories.
Command
ls /etc/cron.*

These directories represent fixed scheduling intervals. Scripts placed here are executed automatically at the corresponding cadence.

/etc/cron.daily/
/etc/cron.hourly/
/etc/cron.monthly/
/etc/cron.weekly/
Step 2 : Create an hourly log cleanup script.
Command
sudo vim /etc/cron.hourly/cleanup_logs
#!/bin/bash
find /var/log -type f -name '*.log' -mtime +3 -delete
Step 3 : Make the hourly script executable.
Command
sudo chmod +x /etc/cron.hourly/cleanup_logs
Step 4 : Create a daily backup script.
Command
sudo vim /etc/cron.daily/backup_home
#!/bin/bash
tar -czf /backup/home_$(date +%F).tar.gz /home
Step 5 : Make the daily backup script executable.
Command
sudo chmod +x /etc/cron.daily/backup_home
Step 6 : Create a monthly sync script.
Command
sudo vim /etc/cron.monthly/monthly_sync
#!/bin/bash
rsync -av /backup/ /mnt/nas/monthly_archive
Step 7 : Make the monthly script executable.
Command
sudo chmod +x /etc/cron.monthly/monthly_sync

Common breakpoints

Script present but never runs

Most interval directories require executable scripts. Check permissions with ls -l and confirm the executable bit is set.

Script runs manually but fails in cron

Cron runs with a minimal environment. Use absolute paths and avoid relying on shell profiles or relative directories.

Wrong interval directory used

Confirm the script is in the correct directory. A script in /etc/cron.monthly will not run hourly or daily.

Cleanup checklist

Remove the test scripts to return the system to its original state.

Commands
sudo rm -f /etc/cron.hourly/cleanup_logs
sudo rm -f /etc/cron.daily/backup_home
sudo rm -f /etc/cron.monthly/monthly_sync

Reference

  • ls /etc/cron.* : Lists system-wide cron interval directories and their contents.
  • sudo vim /etc/cron.hourly/cleanup_logs : Creates or edits an hourly cron script.
  • sudo vim /etc/cron.daily/backup_home : Creates or edits a daily cron script.
  • sudo vim /etc/cron.monthly/monthly_sync : Creates or edits a monthly cron script.
  • sudo chmod +x <script> : Makes a cron script executable.
    • +x: adds execute permission.
  • find /var/log -type f -name '*.log' -mtime +3 -delete : Removes log files older than three days.
    • -type f: match files only.
    • -name '*.log': match log filename pattern.
    • -mtime +3: older than 3 days.
    • -delete: remove matched files.
  • tar -czf <archive> /home : Creates a compressed archive of the home directory.
    • -c: create archive.
    • -z: gzip compress.
    • -f: write to file.
  • rsync -av /backup/ /mnt/nas/monthly_archive : Synchronizes backup data to a remote archive location.
    • -a: archive mode.
    • -v: verbose output.