Can I use Cron to schedule a daily commit on macOS?

Viewed 169

I'm currently working on a project that I'd like to commit to a repo on a daily basis as a tertiary backup. I've written the following little script to quickly commit my changes for the day:

REPO_PATH="/Users/niekvandepas/my-project"
git -C "$REPO_PATH" add -A
git -C "$REPO_PATH" commit -m "`date`"

The script works as expected, committing my changes for the day with a timestamp. Now I'd like to be able to automatically run this script on a daily basis. I tried using cron for this, but I'm running into an issue regarding (I think) directory or file permissions.

I've put the above script in /usr/local/bin/commitscript.sh and have verified that it still works. I then added the following line to my user crontab (crontab -e, no sudo):

0 10 * * * /usr/local/bin/commitscript.sh > /tmp/cron.log 2> /tmp/cron.err

However, when 10:00 comes around, my changes are not committed, and /tmp/cron.err has the following lines:

fatal: Unable to read current working directory: Operation not permitted
fatal: Unable to read current working directory: Operation not permitted

I'm assuming both of these are fatal git errors, for git add and git commit, respectively. This is confusing to me, since it's my user crontab, and the git repo resides in my home directory. Any ideas?

By the way, I'm running macOS Big Sur 11.4 and have granted iTerm full disk access.

1 Answers

You probably need to manually give the cron executable the required access as you did with iTerm. This is sometimes necessary because the cron jobs run in the background and might need to be authorised themselves to access certain resources.

To grant /usr/sbin/cron Full Disk Access, you can open System Preferences, navigate to Security & Privacy, select Privacy in the tab-bar and select Full Disk Access on the left sidebar. Then click the + button, search & select the executable /usr/sbin/cron and click Open.

This will add an entry in the list of apps and your cronjob should now have the required permissions. Here is a screenshot of the panel in the System Preferences:

screenshot-of-settings

Related