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

简介: Q:  http://stackoverflow.com/questions/7994663/git-push-via-cron    I'm trying to run a git push from cron.

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/
目录
相关文章
|
2天前
|
开发工具 git
git学习四:常用命令总结,包括创建基本命令,分支操作,合并命令,压缩命令,回溯历史命令,拉取命令
这篇文章是关于Git常用命令的总结,包括初始化配置、基本提交、分支操作、合并、压缩历史、推送和拉取远程仓库等操作的详细说明。
10 0
git学习四:常用命令总结,包括创建基本命令,分支操作,合并命令,压缩命令,回溯历史命令,拉取命令
|
17天前
|
存储 Linux 开发工具
掌握 Git 命令:每个开发者的必备技能
无论团队项目还是个人开发,掌握 Git 命令都是必备技能。本文介绍 Git 的基本概念与命令,如初始化仓库 (`git init`)、添加文件 (`git add`)、提交更改 (`git commit`)、检出分支 (`git checkout`)、合并分支 (`git merge`) 等,还分享了高级技巧如查看差异 (`git diff`)、撤销提交 (`git revert`)、修复合并冲突 (`git mergetool`) 和使用别名简化命令 (`git config --global alias.ci commit`)。
|
20天前
|
机器学习/深度学习 Shell 开发工具
Python使用管道执行git命令报错|4-7
Python使用管道执行git命令报错|4-7
|
20天前
|
存储 Linux 开发工具
Git基础命令,分支,标签的使用【快速入门Git】
本文详细介绍了Git版本控制系统的基础概念和常用命令,包括工作区、暂存区和版本库的区别,文件状态的变化,以及如何进行文件的添加、提交、查看状态、重命名、删除、查看提交历史、远程仓库操作和分支管理,还涉及了Git标签的创建和删除,旨在帮助读者快速入门Git。
Git基础命令,分支,标签的使用【快速入门Git】
|
21天前
|
存储 Linux 开发工具
掌握 Git 命令:每个开发者的必备技能
本文介绍 Git 的核心概念,如仓库、提交、分支与合并,并提供了常用命令,如初始化仓库 (`git init`)、提交更改 (`git commit -m "Commit message"`)、拉取 (`git pull`) 和推送 (`git push`) 等。此外,还分享了高级技巧,如撤销提交 (`git revert <commit>`)、交互式暂存 (`git add -i`) 和使用别名简化命令 (`git config --global alias.ci commit`) 等,帮助开发者提升效率。无论是初学者还是资深开发者,都能从中受益。
|
29天前
|
开发工具 git 开发者
GIT命令的综合总结
Git的学习曲线可能比较陡峭,但熟练掌握这些命令后,你将能够更加高效地管理和协作你的项目。希望这份指南能帮助你成为Git的高效用户。
45 7
|
18天前
|
开发工具 git
深入理解Git中的git pull和git fetch命令
深入理解Git中的git pull和git fetch命令
22 0
|
2月前
|
存储 开发工具 git
Git常用命令汇总
这是Git命令速查表,涵盖从版本库创建、文件添加与提交、状态查询到分支管理、标签创建及撤销操作的各项常用指令。同时介绍了如何通过GitHub进行代码仓库的创建与同步,帮助用户高效地使用Git进行版本控制和协作开发。
Git常用命令汇总
|
2月前
|
开发工具 git 开发者
Git进阶:超越pull与push,掌握这5条提效命令
【8月更文挑战第27天】在软件开发的世界里,Git作为版本控制的佼佼者,几乎成为了每位开发者的必备工具。然而,许多初学者甚至资深开发者在日常使用中,往往局限于git pull和git push这两个基础命令,忽略了Git强大的功能和灵活性。今天,我们就来探索五条能够显著提升你Git使用效率的进阶命令,让你的版本控制之路更加顺畅。
57 0
|
2月前
|
存储 网络安全 开发工具
Git 协同开发详解:从基础命令到多人协作
Git 协同开发详解:从基础命令到多人协作
34 0

相关实验场景

更多