hg pull from Bitbucket fails when run from cron

I have a local copy of a Bitbucket repo on one of my servers (under the user rob), and I created a script that runs every hour in cron and attempts to pull & update the local copy. The issue I'm having is that hg pull fails, even though when I follow the same procedure outside of cron it works fine.

I setup an ssh key for accessing Bitbucket in the normal fashion (i.e. followed the guide in the Bitbucket docs) and then the repo was initially cloned with

cd /home/rob
hg clone ssh://[email protected]/robjohncox/tools

The update script at /home/rob/bin/update_tools.sh is:

#! /bin/bash
cd /home/rob/tools
hg pull >> /tmp/update_tools.log
hg update >> /tmp/update_tools.log

And the crontab for user rob is:

0 * * * * /home/rob/bin/update_tools.sh

All very simple. However, when the job runs, we see that the call to hg pull fails with the output

remote: Permission denied (publickey).

Does anyone have an idea why this may be happening - is there some other step that I need to take so that when cron runs it has access to the relevant public key? The update_tools.sh script runs fine from a shell.


Try specifying the ssh command explicitly e.g.

hg pull -e 'ssh -i /location/of/key'

Cron runs with a minimal shell, so won't have access to your ssh-agent process because the SSH environment variables aren't set.


Try adding sometimng like the following to your ~/.ssh/config file

Host example.com
IdentityFile ~/.ssh/id_bitbucket

to tell ssh to use a different key for the relevant server.