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 the kind of baseline automation that prevents log sprawl, keeps backups consistent, and makes routine maintenance repeatable without someone manually running commands.

Objective

  • Navigate and identify system-wide cron interval directories.
  • Create an hourly maintenance script in /etc/cron.hourly .
  • Create a daily backup script in /etc/cron.daily .
  • Create a monthly archive sync script in /etc/cron.monthly .
  • Verify placement by listing /etc/cron.* and confirming scripts exist and are executable.

What You’ll Practice

  • Understanding interval-based scheduling via /etc/cron.hourly , /etc/cron.daily , /etc/cron.weekly , and /etc/cron.monthly .
  • Creating small, realistic automation scripts with vim .
  • Enforcing executable permissions using chmod +x so cron can run your scripts.
  • Building maintenance workflows using find , tar , and rsync in scripts.
  • Troubleshooting placement and visibility using ls to confirm interval directories and scripts.

Walkthrough

Step 1 : Navigate to system-wide cron interval directories.
Command
cd /etc/cron.*

The interval directories under /etc are the system-wide drop-in locations for recurring tasks. On many systems, scripts placed here are executed by the system cron framework at the matching frequency.

# Common directories:
# /etc/cron.hourly
# /etc/cron.daily
# /etc/cron.weekly
# /etc/cron.monthly
Step 2 : Create an hourly log cleanup script.
Command
sudo vim /etc/cron.hourly/cleanup_logs

Hourly tasks are for small, frequent maintenance operations. This example removes older log files to reduce disk growth. Keep these scripts fast and predictable.

#!/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

Cron interval directories generally require executable scripts. If the file is not executable, it may be skipped even if it exists in the correct location.

# Confirm executable bit:
# -rwxr-xr-x ... /etc/cron.hourly/cleanup_logs
Step 4 : Create a daily backup script.
Command
sudo vim /etc/cron.daily/backup_home

Daily tasks are a good fit for backups and reports. This script generates a dated archive of /home to a backup location.

#!/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

If you ever wonder why a cron directory script is not running, executable permissions are one of the first things to check.

# Confirm executable bit:
# -rwxr-xr-x ... /etc/cron.daily/backup_home
Step 6 : Verify all cron interval directories and contents.
Command
ls /etc/cron.*

This is your fast placement check. Confirm scripts exist in the intended directories and remember that /etc/cron.weekly and /etc/cron.monthly are also valid drop-in targets for longer cadence tasks.

/etc/cron.daily/backup_home
/etc/cron.hourly/cleanup_logs
/etc/cron.weekly/
/etc/cron.monthly/
Step 7 : Create a monthly sync script.
Command
sudo vim /etc/cron.monthly/monthly_sync

Monthly tasks are useful for archive rollups and long-term retention workflows. This example syncs backups to a NAS location.

#!/bin/bash
rsync -av /backup/ /mnt/nas/monthly_archive
Step 8 : Make the monthly sync script executable.
Command
sudo chmod +x /etc/cron.monthly/monthly_sync

The executable bit is required here as well. Once set, this script is eligible to run on the monthly schedule provided by your system cron configuration.

# Confirm executable bit:
# -rwxr-xr-x ... /etc/cron.monthly/monthly_sync

Reference

  • /etc/cron.hourly : Drop-in directory for scripts executed on an hourly basis.
  • /etc/cron.daily : Drop-in directory for scripts executed daily.
  • /etc/cron.weekly : Drop-in directory for scripts executed weekly.
  • /etc/cron.monthly : Drop-in directory for scripts executed monthly.
  • vim : Create or edit maintenance scripts.
  • chmod +x <file> : Marks a script as executable so it can be run by the cron framework.
  • ls /etc/cron.* : Verifies directory presence and confirms script placement.
  • find : Used for log cleanup operations (filter files and delete old logs).
  • tar : Creates compressed archives for backups.
  • rsync : Synchronizes files and directories for archive replication.