My Adventure of adding a GIT pull cron job on Mac OS Sierra

My Adventure of adding a GIT pull cron job on Mac OS Sierra

I had requirement to periodically check for updates on in a git repository so that a tool that I developed had the most up to date templates so the configuration would generated correctly.

Note: Scroll to the bottom for the answer.

First, I attempted to just add the cron job.

I added this code.

  1. crontab -e
  2. */1 * * * * cd /Users/dazfre/PycharmProjects/Django-networktool/mysite/forms/scripts/templates && /usr/local/bin/git -q pull origin master
  3. save and exit :wq

When the job ran, I got this message.

/bin/sh: cd /Users/dazfre/PycharmProjects/Django-networktool/mysite/forms/scripts/templates && /usr/local/bin/git -q pull origin master: No such file or directory

This is when I found out that the -q for git was causing the directory issue. I removed it, added it with out it and got this error message.

“`Host key verification failed.

fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.“`

OK, so I found out that cron job is a very dumbed down shell and doesn’t have access to my SSH KEY and doesn’t even know where it is. Also, it doesn’t know where the SSH AGENT is.

Since, unfortunately there is no -i option for git to specify what SSH key you want to use. There are some scripts that you can make, but I didn’t want to make something I didn’t understand, so I did this.

Second, I attempted to tell cron job to setup a SSH agent per job call with a shell script.

I added this code.

  1. create a script nano gitpull.sh

  2. add the guts

  3. cd /Users/dazfre/PycharmProjects/Django-networktool/mysite/forms/scripts/templates

ssh-agent bash -c 'ssh-add /Users/dazfre/.ssh/id_rsa && git pull'

  1. Exit and save ctrl + x yes

  2. Make file executable chmod a+x gitpull.sh

  3. crontab -e

  4. */1 * * * * /Users/dazfre/gitpull.sh

  5. save and exit :wq

Will it work? Nope.

This error message is now popping up.

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

What’s an X Crun?

Since, I am using a Mac and updated to Sierra and it want’s you to accept a user agreement. I’ve read mixed results on the —reset option.

I ran this command.

xcode-select —install

Finally, It worked!

About Daniel Fredrick

Technology enthusiast, Programmer, Network Engineer CCIE# 17094

View all posts by Daniel Fredrick →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.