Pushing to GitHub using a cron job — Permission de

2019-05-06 04:33发布

问题:

I have created an SSH key (following the official tutorial), added it to GitHub and created a Bash script that commits and pushes a single file to my repository on Github. When I run this script from the command line, everything works fine and the updates are pushed. However, when I set up a job using crontab -e, the push generates the following error:

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

I have edited the user's crontab (crontab -e), i.e. I'm NOT using sudo crontab -e. I'm running Ubuntu 12.04.

回答1:

if it isn't a user issue (where you run the job as root, missing the right $HOME/.ssh folder), it can be a passphrase issue:

turns out I was mistaken, and the ssh key was password protected (with keychain loading the ssh-agent), hence why it failed from a script but not when running from the bash session.
Adding . ~/.keychain/$HOSTNAME-sh to my script resolved the problem.

The passphrase bit is detailed in "Not able to ssh in to remote machine using shell script in Crontab":

You can make ssh connections within a cron session. What you need is to setup a public key authentication to have passwordless access.
For this to work, you need to have PubkeyAuthentication yes in each remote server's sshd_config.