bash linux 脚本 git pull 错误:无法打开 .git/FETCH_HEAD:权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16403338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linux scripted git pull error: cannot open .git/FETCH_HEAD: Permission Denied
提问by idonaldson
Need a little help scripting a git pull.
需要一些帮助来编写 git pull 脚本。
We have an intranet server that we are trying to script a git pull to, and I'm running into a permission issue.
我们有一个内部网服务器,我们正在尝试编写 git pull 脚本,但我遇到了权限问题。
error: cannot open .git/FETCH_HEAD: Permission denied
We've setup a user account on the server, cloned the project to that account, chmod 775 -R the directory tree, and setup the ssh key so there is no password prompt.
我们已经在服务器上设置了一个用户帐户,将项目克隆到该帐户,chmod 775 -R 目录树,并设置了 ssh 密钥,因此没有密码提示。
The script looks like so:
脚本如下所示:
#! /bin/bash
sudo /bin/su - gitrepouser
cd /some/directory/for/git && git pull
When I run the script as another user, it runs the sudo command, and stops, after typing exit and I get the error message.
当我以另一个用户身份运行脚本时,它会运行 sudo 命令,然后在输入 exit 后停止,我收到错误消息。
This script will be called by our batch process on a 10 minute interval, pulling any production changes down from BitBucket.
该脚本将由我们的批处理以 10 分钟的间隔调用,从 BitBucket 中提取任何生产更改。
Thoughts?
想法?
回答by Fernando Diaz Garrido
You probably need to make the user the owner of the folder
您可能需要使用户成为文件夹的所有者
chown gitrepouser -Rf /some/directory/for/git
回答by Cairnarvon
The problem is that sustarts its own shell; the second command isn't executed in that shell, but in the original one. You can demonstrate this by running the following script:
问题是su启动了自己的shell;第二个命令不是在那个 shell 中执行,而是在原始命令中执行。您可以通过运行以下脚本来演示这一点:
#!/bin/bash
su
whoami
Unless you're already root when you run it, it won't output root.
除非你在运行它时已经是 root,否则它不会输出root.
The solution is to use su's -cswitch:
解决方案是使用su的-c开关:
sudo /bin/su -c "cd /some/directory/for/git && git pull" - gitrepouser

