Git 使用 cronjob 自动拉取
我试图创建一个 cronjob,其任务是每分钟执行一次 git pull 以使我的生产站点与主分支保持同步。
由于权限问题,git pull需要由系统用户nobody
来完成。然而,似乎 nobody
帐户不允许运行命令。所以我必须以 root
用户身份创建任务。
我尝试过的 crontab 条目:
*/1 * * * * su -s /bin/sh nobody -c 'cd ~heilee/www && git pull -q origin master' >> ~/git.log
它不起作用,我不知道如何调试它。
有人可以帮忙吗?
UPDATE1: git pull 命令本身是正确的。我可以毫无错误地运行它。
I was trying to create a cronjob with a task to do a git pull
every minute to keep my production site in sync with my master branch.
The git pull needs to be done by the system user nobody
, due to the permissions problem. However it seems that the nobody
account is not allowed run commands. So I have to create tasks as the root
user.
The crontab entry I tried:
*/1 * * * * su -s /bin/sh nobody -c 'cd ~heilee/www && git pull -q origin master' >> ~/git.log
It doesn't work, and I don't know how to debug it.
Could anyone help?
UPDATE1: the git pull
command itself is correct. I can run it without errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
遇到同样的事情,我强烈建议像这样拉分支:
这样无论你在哪个分支,它都会拉原始主机并合并到本地主机中。不会签出你,不会中断你的工作(除非你可能会遇到 git lock,无论如何,考虑到你每半小时拉一次,这不会很长)。
另外,整个字符串(每个工作日,每半小时+每小时):
如果您需要使 monorepo 更快,这确实是一件好事。
Encountered the same thing, and I highly recommend pulling the branch like this:
So that it pulls origin master and merges into local master regardless on what branch you're in. Doesn't checkout you, doesn't interrupt your work (except you may encounter a git lock, which isn't gonna be long anyway, considering you pull it every half an hour).
Also, the whole string (every working day, every half an hour + every hour):
If you need to make a monorepo faster, it's a really good thing.
解决方案:
Solution:
虽然您确实需要首先弄清楚如何使更新正常工作,但最好使用上游的钩子来使其运行。您可以简单地使用
post-commit
钩子中的curl 来完成此操作,或者如果您使用的是 github,则只需在其一侧使用 post-receive 钩子即可。While you do need to figure out how to get the update to work in the first place, you'd be far better off using a hook from the upstream to make it go. You can do this simply with curl from a
post-commit
hook or if you're using github, just use a post-receive hook on their side.这纠正了一些错误,这些错误导致接受的答案无法在我的系统(Ubuntu > 10.04 服务器)上运行。关键的变化似乎是在
pull
之后而不是之前的-q
。在尾部/var/log/syslog
文件或尝试运行未更新的生产代码之前,您不会注意到拉取不起作用。This corrects a couple errors that prevented the accepted answer from working on my system (Ubuntu >10.04 server). The key change seems to be the
-q
after thepull
rather than before. You won't notice that your pull isn't working until you tail the/var/log/syslog
file or try to run your non-updated production code.从 Git 版本开始 1.8.5 (Q4 2013) 您可以使用 -C 参数轻松完成此操作。然后,您不需要 cd 进入 Git 项目目录,而是可以将其指定为 Git 命令的一部分,如下所示:
Starting from Git version 1.8.5 (Q4 2013) you can use the -C argument to easily accomplish this. Then you don't need to cd into the Git project directory but can instead specify it as part of the Git command like this:
在撰写此答案时,所有建议的解决方案都以
cd
进入工作目录开始。就我个人而言,我更喜欢像这样使用 git-dir 参数。出于我自己的目的,我使用这个 cron 来使我的本地开发盒与其他开发人员保持同步。因此,我使用
fetch
而不是pull
为了简化当前接受的解决方案,使用
git-dir
参数At the time of writing this answer, all the suggested solutions start with
cd
'ing into the working directory. Personally I would prefer to use thegit-dir
argument as so.For my own purposes, I'm using this cron to keep my local dev box up to date with what the other developers. Hence I'm using
fetch
instead ofpull
To simplify the current accepted solution, using the
git-dir
argument那是我一直在使用并且有效的
that is has been using by me and worked
我创建了一个小脚本来处理它。然后可以使用 crontab 命令
这里是 gitpull.sh :
我们必须设置工作目录
I create a small script to deal with it.Then can use the by command crontab
Here are the
gitpull.sh
:We have to set the work directory