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.
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:
- Open System Preferences
- Open the Security and Privacy preference pane
- Select the Privacy tab and choose Full Disk Access from the left pane
- Click the lock at the bottom of the window to enable changes, authenticate (admin rights required)
- In the right pane, click the + button; a file selection dialog should appear
- Press Cmd-Shift-G to open the Go to folder dialog; enter
/usr/sbin
and click Go - Find the file
cron
in the selection dialog, select it, and click Open - 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.
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)