crontab定时运行git命令 更新代码库

Q:  http://stackoverflow.com/questions/7994663/git-push-via-cron

   I'm trying to run a git push from cron. When I do the command interactively on the shell it's going through fine. When running the command from my user's crontab, cron delivers the error message
Permission denied (publickey).

I presume it hasn't to do with finding or reading my ~/.ssh/id_rsa, as I can cat the file from cron alright. UID and EUID are set fine in the cron job. - Any ideas?

UPDATE

I got it working when supplying the environment key SSH_AUTH_SOCK to my cron job, but I'm concerned that this is only valid as long as I'm logged in. I'm looking for a solution that works independent of interactive logins.

A:

 

down vote accepted

As explained here, it can be due to the lack of knowledge from the cron session shell of the ssh agent.
If that is the case (ie if you are using private ssh keys with a passphrase), keychain is the usual solution (as mentioned here).
More details in this example: "Passwordless connections via OpenSSH using public key authentication, keychain and AgentForward".

 

 

 

Very brief synopsis

 

1. Generate private/public key pair (id_dsa and id_dsa.pub).

Save private key in ~/.ssh/id_dsa on trusted hosts you will ssh from.

Append public key to ~/.ssh/authorized_keys on hosts you will ssh to.

Do not use ~/.ssh/authorized_keys2.  If you see that file, remove it

and upgrade to a recent version of SSH.

 

2. Set ForwardAgent yes in ssh_config on all hosts you ssh

from.  Set PubkeyAuthentication yes in sshd_config on all

hosts you ssh to.  Maybe set StrictModes no in sshd_config.

 

3. Install keychain then configure by adding the following lines

to your $HOME/.bash_profile:

 

    /usr/local/bin/keychain $HOME/.ssh/id_dsa

    source $HOME/.keychain/${HOSTNAME}-sh

 

4. For crontab scripts that use ssh, add the following line in

order to avoid having to manually enter a passphrase:

 

    source $HOME/.keychain/${HOSTNAME}-sh

 

5. ssh from host to host without needing to enter passwords.  Where

you can ssh, you can also scp, sftp, etc., all securely but without

having to enter your passwords on the various hosts you use.

如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值