Automate running a script using crontab on macOS

Herman Li
Mac O’Clock
Published in
3 min readApr 9, 2020
Photo by Henan Li

What is cron

The cron is a time-based job scheduler in Unix-like computer operating systems. It allows the user to schedule time to run tasks, so you can automate things like backups, create logs, health checks, etc.

Before config the crontab file

Operation not permitted

The macOS Mojave 10.14.0 release introduces some updates to the Apple user-centric inter-app data-sharing security model. After that user has to give access to cron to the calendar and certain directories, otherwise the crontab command will result in error message shown in the subtitle.

Full Disk Access

  • Go to System Preferences > Security & Privacy > Privacy > Full Disk Access:
  • Click on the (+) icon to add an item to the list:
  • Press command+shift+G then type /usr/sbin/cron and Enter, you can find cron in finder:
  • Select the cron file and click Open:

Config crontab

Go to the crontab file

  • Go to the Terminal or other command-line interface you prefer, type crontab -e. If your crontab file does not yet exist, this command will create the file for you
  • Optional: you can choose your preferred editor like nano, just type env EDITOR=nano crontab -e

Write cron command

  • Syntax:

A cron command first specifies the time interval that you would like to run the script then specifies that command to execute. The syntax is shown below:


* * * * * command_to_executed
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
  • Examples:

Run a python script in every minute: * * * * * python /path/to/hello.py Run a python script every 10 minutes: */10 * * * * python/path/to/hello.py Run a python script every day at 12.30: 30 12 * * * python/path/to/hello.py

  • Save and Exit:

In nano, the user can press command+O to save changes, and press command+X to exit.

  • Check and Remove:

After writing the cron command, the user can type crontab -l in Terminal to display current crontab or remove the current crontab by typing crontab -r in Terminal.

  • The crontab guru provides a handy option to check the cron command and more examples:

Potential problems for Python

Python path

  • Some users may have different Python versions and the default Python may not work for the current script. So the user may have to specify the Python path in the cron command like:
* * * * * /Users/user_name/anaconda3/bin/python /path/to/hello.py
  • You can also add Python path in the Python script like:
#!/Users/user_name/anaconda3/bin/python
# Your Python script goes here
print("Hello World")

Set a working directory

  • Sometimes, the user may want to set a working directory before executing the Python script. You can use && to combine different commands:
* * * * * cd /path/to/dir && python /path/to/hello.py

Generate log

  • You can also generate a log to better locate and debug potential issues by this:
* * * * * python /path/to/hello.py >> /dir_to/crontest.log 2>&1

--

--