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.
-
Write the Script: First, create a script to perform the action.
nano ~/cleanup_downloads.shAdd the following content:
#!/bin/zsh # Delete files in Downloads older than 30 days find ~/Downloads -type f -mtime +30 -deleteMake it executable:
chmod +x ~/cleanup_downloads.sh -
Schedule the Job: Run
crontab -eand add the following line to run the script every Monday at 3:00 AM:0 3 * * 1 /Users/yourusername/cleanup_downloads.shNote: Replace
yourusernamewith your actual account name.
Permissions and Security
In macOS Tahoe, security is strict. The cron daemon needs permission to access your files.
- Open System Settings > Privacy & Security > Full Disk Access.
- Click the
+button. - Press
Cmd + Shift + Gand type/usr/sbin/cron. - Select
cronand 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