PUBLISHED: December 28, 2020
Deprecation Notice: This article was written more than a year ago which means that its information might no longer be up-to-date. We cannot therefore guarantee the accuracy of it's contents.

Table of Contents


How to schedule tasks with crontab on macOS

Cron is a time-based job scheduler that is available on Unix-like computer operating systems such as macOS. The actions of cron are driven by a crontab (cron table) file, that specifies shell commands to run periodically on a given schedule. This article explains how you can schedule tasks with crontab on macOS.


The anatomy of a crontab file

Each line of a crontab file represents a job and looks like this:

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of the week (0 - 6) 
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │

* * * * * command

The syntax of each line expects a cron expression made of five fields (minute, hour, day of the month, month, day of the week) which represent the time to execute the command, followed by a shell command to execute.


Editing the crontab

To edit (and create) a crontab file, open a new Terminal and type the following command:

crontab -e

Schedule tasks with crontab macOS - Image 1

The crontab will open in an editor of your choice, which by default is vim. For this example, let us assume that we just want to print hello world into a text file at 12:15 on each day of the year. The syntax would look like this:

15 12 * * * echo hello world >> /Users/geek/result.txt

Schedule tasks with crontab macOS - Image 2

Once you save and close, the crontab file is installed (but not yet guaranteed to be running).


Giving cron full disk access

If you are using macOS Catalina (or higher) you will also need to give the cron service Full Disk Access.

Go to the System Preferences and select Security & Privacy.

Schedule tasks with crontab macOS - Image 3

In the left-hand column, select Full Disk Access, click the padlock icon, enter your password and click on the plus icon for a file browser to appear.

Schedule tasks with crontab macOS - Image 4

When the file browser appears, press ⌘ ⇧ G to open the go to folder dialog and type:

/usr/sbin/cron

Schedule tasks with crontab macOS - Image 5

Press Go to close the dialog and confirm that cron appears in the list of apps that have full disk access in the Security & Privacy window.

Schedule tasks with crontab macOS - Image 6

Crontab has now been successfully installed on systems running macOS Catalina (and higher).


Displaying the crontab

To display the contents of the crontab, type the following command:

crontab -l

Schedule tasks with crontab macOS - Image 7


Removing the crontab

If you no longer wish to run the crontab, type the following command to remove it:

crontab -r

Schedule tasks with crontab macOS - Image 8


Changing the default editor

It is possible to specify a different editor when you edit the crontab by typing the following command:

export VISUAL=/usr/bin/nano

Schedule tasks with crontab macOS - Image 9

In this instance the default editor was changed to nano.


Summary

This guide has shown how cron, a time-based job scheduler available on Unix-like computer operating systems such as macOS can be used to schedule tasks with a crontab on macOS.


Further information

For more information about the crontab, in a Terminal type:

man crontab

We also recommend visiting the crontab guru website for further examples on how to construct a crontab file.

See Also

How to install and configure Syncthing on the Mac
How to install and configure Syncthing on the Mac

How to restart the Samba (SMB) file sharing service on macOS
How to restart the Samba (SMB) file sharing service on macOS

How to enable case-sensitive file system support in PyCharm for macOS
How to enable case-sensitive file system support in PyCharm for macOS

How to disable Adobe Creative Cloud during startup on the Mac
How to disable Adobe Creative Cloud during startup on the Mac

How to mount external volumes in Docker
How to mount external volumes in Docker

Which components can be upgraded on my Mac?
Which components can be upgraded on my Mac?

How to hide external disk drives from the desktop in macOS
How to hide external disk drives from the desktop in macOS

How to upgrade rsync on macOS Catalina
How to upgrade rsync on macOS Catalina

How to change the startup programs in macOS
How to change the startup programs in macOS

How to install Nextcloud on Mac with Docker
How to install Nextcloud on Mac with Docker



comments powered by Disqus

See also