Category Archives: Git

Setting up Git Pulls in Crontab

I have a small application that reads Git data and processes developer contributions based on lines of code (inserts + deletes), files edited and the number of commits each month. I wanted to automate the Git pull command so that this application would stay up to date without me having to manually run a command-line script.

My first thought was to schedule it in cron so that I didn’t have to worry about it. This is running on my development laptop — at least for now — and I knew that if I ran the “git pull” command under my username, I shouldn’t have any issues.

However, that was not the case. Every time the script was run from cron, it failed with a permission error. Running the same script by invoking it from the command line, however, executed without an issue.

But what was the permission error? Was it that Git could not be invoked? After some serious searching through Google and Stack Overflow, I determined that it was likely an SSH problem. Stack Overflow is filled with comments about using Git hooks and setting up a “bare” repository, etc. That was a lot more complicated and I resolved to just run it by hand.

A day later, though, I discussed this with one our senior developers — who has a much deeper understanding of Git. He agreed that this was an SSH issue. Together, we determined my script — when run from cron — did not have the rights to access my RSA key file, which is how I access my Git server (not GitHub).

So, we created a new RSA key just for this process using ssh-keygen and storing it in a specific directory. The key to this was to hit the enter key each time ssh-keygen asked for a password. This allows the script to pull the repos unattended.

ssh-keygen -f .git-ssh/id_rsa

Once my public key was copied to the Git server, I was then able to modify the pull command in my script to force it use the new ssh key.

ssh-agent bash -c 'ssh-add /Users/username/.git-ssh/id_rsa; git pull'

This was about a five minute process and it works perfectly.

My crontab entry looks like this:

0 9-16 * * 1-5 /Users/username/scripts/  > /path/to/logs/git-perl-pull.txt 2>&1

This will run between 9am and 4pm Monday through Friday.