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://hg@bitbucket.org/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.

  • What is the location and name of your private key ? – user9517 May 6 '14 at 13:24
  • It's /home/rob/.ssh/id_bitbucket (sorry should have mentioned it wasn't id_rsa) – robjohncox May 6 '14 at 13:25
  • I hope I'm not arming you with the knowledge required to hack my server :) – robjohncox May 6 '14 at 13:26
  • To be honest I don't think this is a cron specific issue but work your way through serverfault.com/questions/449651/… - it will help you gather information to help us help you. – user9517 May 6 '14 at 13:33
  • Thanks Iain - someone found a fix to the problem, but that answer you linked looks very valuable, I will go study – robjohncox May 6 '14 at 13:37

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.