整个脚本运行正常。我还要注意,如果我将cron作业复制并粘贴到shell中并手动运行它就没有问题。
Base = '/home/user/git/'
GIT_out = Base + ("git_file.txt")
FILE_NAME = Base + 'rules/file.xml'
CD_file = open(Base + "rules/reports/CD.txt", 'r')
os.chdir(Base + 'rules')
gitFetchPull = "git fetch --all ;sleep 3 ; git pull --all"
git1 = subprocess.Popen(gitFetchPull, shell=True, stdout=subprocess.PIPE)
gitOut = git1.stdout.read()
print(gitOut)
当我从cron读取输出时,它似乎无法进行身份验证
Received disconnect from 172.17.3.18: 2: Too many authentication failures for tyoffe4
fatal: The remote end hung up unexpectedly
error: Could not fetch origin
cron job
* * * /usr/bin/python /home/tyoffe4/git/rules/reports/cd_release.py >/home/tyoffe4/git/rules/reports/cd_release.out 2>&1
答案 0 :(得分:3)
这可能是cron环境没有ssh代理设置环境变量的问题。因此,当git进行ssh连接时,它无法进行身份验证,因为它无法联系您的ssh代理并获取密钥。
这个答案可能有你想要的: ssh-agent and crontab -- is there a good way to get these to meet?
如果由于某种原因,它与ssh-agent无关,请尝试在脚本顶部的print os.environ
转储所有环境变量的值。
比较cron的输出并在bash shell中运行env
。可能存在一些差异,其中一个是您的错误的来源。
如果在shell中设置与cron中相同的环境变量,则行为应该重现。