2 min read

Automating Tasks with Cron on macOS Tahoe

Cron is a time-based job scheduler in Unix-like computer operating systems, including macOS Tahoe. It enables users to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

Understanding Cron

Cron is driven by a configuration file called a "crontab" (cron table). Each line in a crontab represents a job and follows a specific pattern:

* * * * * command_to_execute
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Editing the Crontab

To edit your user's crontab file, open the Terminal (see Getting Started with macOS Tahoe) and run:

crontab -e

This will open the crontab in the default system editor (usually vi or nano).

Creating a Maintenance Task

Let's create a simple automation to clean up the "Downloads" folder every week.

  1. Write the Script: First, create a script to perform the action.

    nano ~/cleanup_downloads.sh

    Add the following content:

    #!/bin/zsh
    # Delete files in Downloads older than 30 days
    find ~/Downloads -type f -mtime +30 -delete

    Make it executable:

    chmod +x ~/cleanup_downloads.sh
  2. Schedule the Job: Run crontab -e and add the following line to run the script every Monday at 3:00 AM:

    0 3 * * 1 /Users/yourusername/cleanup_downloads.sh

    Note: Replace yourusername with your actual account name.

Permissions and Security

In macOS Tahoe, security is strict. The cron daemon needs permission to access your files.

  1. Open System Settings > Privacy & Security > Full Disk Access.
  2. Click the + button.
  3. Press Cmd + Shift + G and type /usr/sbin/cron.
  4. Select cron and ensure the toggle is enabled.

Without this step, your cron jobs may fail silently when trying to access protected folders like Downloads or Documents.

Viewing Active Jobs

To list the jobs currently scheduled for your user:

crontab -l