Skip to content

Instantly share code, notes, and snippets.

@darrenpmeyer
Created November 8, 2021 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenpmeyer/67b80904bf78b2bf8a450ca0631fb96a to your computer and use it in GitHub Desktop.
Save darrenpmeyer/67b80904bf78b2bf8a450ca0631fb96a to your computer and use it in GitHub Desktop.
Setting up user cron jobs on macOS 11.6 Big Sur

NOTE the cron command scheduler is considered deprecated by Apple, in favor of using Launch Agents along with launchd. There is a decent launchd tutorial at launchd.info if you want to do that instead.

Give cron full disk access

Cron jobs usually need to work outside of the macOS "sandbox", accessing scripts and files across the filesystem. macOS supports Unix filesystem permissions, but there's an additional safety gate that's evaluated before those permissions are considered at all. To allow cron (and therefore cron scripts) to pass this gate, it must be allowed Full Disk Access:

  1. Open System Preferences
  2. Open the Security and Privacy preference pane
  3. Select the Privacy tab and choose Full Disk Access from the left pane
  4. Click the lock at the bottom of the window to enable changes, authenticate (admin rights required)
  5. In the right pane, click the + button; a file selection dialog should appear
  6. Press Cmd-Shift-G to open the Go to folder dialog; enter /usr/sbin and click Go
  7. Find the file cron in the selection dialog, select it, and click Open
  8. Verify that cron appears in the right pane of the preferences window (if not, repeat from step 5)

The cron executable now has the ability to access the disk, subject to filesystem permissions.

Author a crontab

export EDITOR=/usr/bin/nano
crontab -e

NOTE set EDITOR to any unix editor that will block; I use /usr/bin/vim for example -- nano is used here because it's relatively easy to use

crontab -e edits your user crontab file; one schedule per line. See the crontab manual page (or man 5 crontab for the local copy of that manual page), but the basic format for user crontabs is:

* * * * *  Command_to_execute
- – – – -
| | | | |
| | | | +—– Day of week (0–7) (Sunday=0 or 7) or Sun, Mon, Tue,…
| | | +———- Month (1–12) or Jan, Feb,…
| | +————-— Day of month (1–31)
| +——————– Hour (0–23)
+————————- Minute (0–59)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment