%% Related: [[Fork readwise2directory]] [[Software Development]] [[macOS]] [[crontab]] %% ## Give cron full system access If you haven't already done it, you'll need to make sure `cron` has the permissions to run scripts. In System Preferences > Security & Privacy > Privacy > Full Disk Access, add `/usr/bin/cron` and make sure it has access. ## Open crontab `env EDITOR=nano crontab -e` ## Set a schedule Add a line to crontab for every schedule you want to create. `0 20 * * * python3 /Users/nic/git/readwise2directory/readwise-GET.py` ### Date and time The first five "words", separated by spaces, correspond to the time periods you can set a script to run. - minute - hour - day of the month - month - day of the week If a certain time period is not required to set the schedule, use a `*` in its position. There should always be 5 parameters for the time, regardless of how many you set. The example above will execute the script every day at 20:00. ### Command The rest of the line contains the command that you actually want to execute. ## Python issues If you're having trouble getting the instructions here to work, and it's a [[Python]] script you're trying to run on a schedule, try the following suggestions. ### Verify the Python version you want to use It may be that cron is trying to run a different version of Python from the one you actually want to run. To check where your Python installation is, run `which python3`. Take note of the path. ### In the cron job, add the Python path For example: `48 23 * * * cd /Users/nic/git/readwise2directory/ && /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 /Users/nic/git/readwise2directory/readwise-GET.py >> /Users/nic/git/readwise2directory/readwiseGET.log` ### Output to a log In crontab, add `>> /path/somelog.log` to see what's happening when cron tries to run the script. See the previous section to see an example. ### cd to the relevant directory Sometimes it helps to add a `cd` to the script directory within the cron command. See above for an example. ## References - https://medium.com/macoclock/automate-running-a-script-using-crontab-on-macos-88a378e0aeac - http://theautomatic.net/2020/11/18/how-to-schedule-a-python-script-on-a-mac/