8

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?

Community
  • 1
  • 1
xfx
  • 1,918
  • 1
  • 19
  • 25
  • Does your ssh private key have a passphrase? If yes, when/how do you normally enter the passphrase to unlock the key? – janos Dec 31 '13 at 07:25
  • @janos no passphrase, so I don't have to enter the password.I want it automatic. – xfx Dec 31 '13 at 07:31
  • You say the script works when you run in the shell, but not when you run in cron. Is cron running as the same user or different one? For example, maybe you run in the shell with user `jack`, but in cron you run it with user `root` ? – janos Dec 31 '13 at 07:36
  • @janos same user, I edit with `crontab -u jack -e`, and `crontab -l` contains that job. – xfx Dec 31 '13 at 07:40
  • @xfx note that the -u option is given, it specifies the name of the user whose crontab is to be tweaked, not that the cron will be run by anyone else but root. – VonC Dec 31 '13 at 07:56
  • @NBhargav yes, by the answer of VonC – xfx Jan 02 '14 at 02:22

2 Answers2

4

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

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Still not working, here is the mail I got when run `ssh -Tvvv` From: root@myserver.localdomain (Cron Daemon) To: jack@myserver.localdomain Subject: Cron /home/jack/cron.sh Content-Type: text/plain; charset=UTF-8 Auto-Submitted: auto-generated X-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: Message-Id: <20131231075601.59C95C0218@myserver.localdomain> Date: Tue, 31 Dec 2013 15:56:01 +0800 (CST) /bin/sh: /home/jack/cron.sh: Permission Denied – xfx Dec 31 '13 at 07:51
  • @xfx you can edit your answer to include the full output of the ssh -Tvvv command. That will be easier to debug. – VonC Dec 31 '13 at 07:53
  • @xfx it seems /home/jack/cron.sh is not executable... `chmod +x` on it and try again – janos Dec 31 '13 at 07:54
  • @xfx and what is your remote url for origin? – VonC Dec 31 '13 at 07:57
  • @VonC hold on, I am trying. – xfx Dec 31 '13 at 08:13
  • Yes! What was missing? – VonC Dec 31 '13 at 08:15
  • @VonC, my key name is not id_rsa, it's id_ras_jack, so ssh cannot find it. I am search for config the name of key now. – xfx Dec 31 '13 at 08:17
  • @xfx stick with the default name, or use a ~/.ssh/config file, as mentioned below. – VonC Dec 31 '13 at 08:31
4

Based on your comments, I don't really understand why your script wouldn't work in cron. We can try a few things to clear things up.

Add a configuration in ~/.ssh/config for the user running the cron job like this:

Host github-project1
User git
HostName github.com
IdentityFile /path/to/github.project1.key
#or like this:
#IdentityFile ~/.ssh/github.project1.key

Then in your git working tree add a new remote like this:

git remote add github-project1 github-project1:USER/PROJECT.git

In the url there, github-project1:user/project.git, change only USER and PROJECT to the right values for your project, but leave github-project1 as is: it must match the value of the Host setting in the ssh configuration we just added.

Finally, change the script to use this new remote:

#!/bin/bash
git push github-project1 master

Test the script first in the shell, and then in cron.

janos
  • 120,954
  • 29
  • 226
  • 236