Search code examples
gitsshcron

cron git push with ssh key


I setup a ssh key for github account, so I don't have to enter the password every time, it works fine. Here is the script I use:

#!/bin/bash
git push origin master

But when I use cron to run it, it will not use my ssh key. Here is the output:

Permission denied (publickey)
fatal: The remote end hung up unexpectedly

I search around and found some articles, but none of them solve my problem. Here are the articles I found(and a lot more):

https://askubuntu.com/questions/110565/why-is-git-not-using-my-pubkey-with-crontab

https://unix.stackexchange.com/questions/35466/how-to-perform-git-push-using-crontab

git push via cron

Can anybody give me a step to step instructions to solve this problem?


Solution

  • As mentioned in one of your thread, you need to point the root user which executes your cron script to the right HOME (the one which contains $HOME/.ssh/id_rsa(.pub), your public and private keys.

    #!/bin/bash
    HOME=/home/yourAccount git push origin master
    

    If that doesn't work, start debugging your ssh command with

    #!/bin/bash
    HOME=/home/yourAccount ssh -Tvvv yourGitServer
    

    And check that with first a simple private key (not protected by a passphrase).
    Then, if you need a passphrase, make sure your ssh-agent is running in order to cache said passphrase (or using keychain, as I mentioned before).

    According to your logs, the public ssh key is proposed, but rejected.

    debug1: Trying private key: /home/jack/.ssh/id_rsa
    debug3: no such identity: /home/jack/.ssh/id_rsa
    

    Double-check "BitBucket Set up SSH for Git", and make sure your id_rsa and id_rsa.pub are there, with the right protection.

    Check also your id_rsa.pub has been added to your BitBucket account (as one line).

    https://confluence.atlassian.com/download/attachments/304578655/ssh_key.png?version=2&modificationDate=1379979345091&api=v2&effects=drop-shadow