如何通过 cron 同步 Git-Svn 克隆?

发布于 2024-10-16 01:41:27 字数 1837 浏览 5 评论 0原文

作为参考,我在服务器上运行 CentOS 5.5。

我正在与其他几位开发人员一起开发一个分布式项目,我们使用 Git 来管理我们的存储库。一切都在 Gitorious 上实时托管并且运行良好。然而,我们的存储库中需要一个分支来与外部 Subversion 存储库保持同步 - 有点像 Git 中的“供应商分支”。原因是:我们正在基于这个现有的开源项目(WordPress)构建一些东西,并希望我们的工作副本继承他们在自己的存储库中发布的任何错误修复。

我已经进行了一些设置,以便我们有一个名为“wordpress”的单独分支,可以与实时 Subversion 存储库保持同步。要做到这一点,很简单:

git checkout wordpress
git svn rebase
git gc
git push origin wordpress

这些命令将:

  1. 确保我们位于 WordPress 分支上
  2. 将分支与实时 Svn 存储库重新同步
  3. 运行垃圾收集(否则存储库逐渐变得难以管理)
  4. 将更新的分支推送到我们的Gitorious 上的托管存储库

每当 WordPress 主干更新时运行这四个命令很容易,但这很烦人和麻烦。我很想将其设置为 cron 作业,但我似乎不知道如何做!

我编写了一个名为 wordpress.sh 的脚本,并将其放置在服务器上的 /etc/ 文件夹中:

#!/bin/bash
cd /var/www/git/wordpress
git checkout wordpress
git svn rebase
git gc
git push origin wordpress

该脚本运行良好。我可以通过 SSH 连接到我的服务器,以 root 身份登录,然后输入 。 /etc/wordpress.sh 一切正常。但我该如何设置才能让 cron 为我做事呢?

我尝试过手动将其添加到 crontab (不成功),使用 CentOS 中的 Webadmin 界面将其添加为每小时脚本(不成功),现在我不知所措!我的系统记录声称它正在触发(日志如下):

Feb  4 07:00:01 ---- crond[27745]: (root) CMD (/etc/wordpress.sh    #WordPress mirror)
Feb  4 07:01:01 ---- crond[27773]: (root) CMD (run-parts /etc/cron.hourly)
Feb  4 08:00:01 ---- crond[29095]: (root) CMD (/etc/wordpress.sh    #WordPress mirror)
Feb  4 08:01:01 ---- crond[29142]: (root) CMD (run-parts /etc/cron.hourly)
Feb  4 09:00:01 ---- crond[29708]: (root) CMD (/etc/wordpress.sh    #WordPress mirror)
Feb  4 09:01:01 ---- crond[29718]: (root) CMD (run-parts /etc/cron.hourly)
Feb  4 10:00:01 ---- crond[30328]: (root) CMD (/etc/wordpress.sh    #WordPress mirror)
Feb  4 10:01:01 ---- crond[30337]: (root) CMD (run-parts /etc/cron.hourly)

但除非我手动登录并运行脚本,否则实际上什么都不会发生。有想法吗?

For reference, I'm running CentOS 5.5 on the server.

I'm working on a distributed project with several other developers, and we're using Git to manage our repositories. Everything's hosted live on Gitorious and working beautifully. However, we need a branch in our repository to stay synchronized with an external Subversion repo - kind of like a "vendor branch" within Git. Reason is: we're building several things based on this existing open source project (WordPress) and want our working copy to inherit any bugfixes they publish in their own repo.

I've got things set up so that we have a separate branch called "wordpress" that can be kept in sync with the live Subversion repo. To do so, it's as simple as:

git checkout wordpress
git svn rebase
git gc
git push origin wordpress

These commands will:

  1. Make sure we're on the WordPress branch
  2. Re-sync the branch with the live Svn repo
  3. Run garbage collection (otherwise the repository progressively gets unmanageably large)
  4. Push the updated branch to our hosted repository on Gitorious

It's easy enough to run these four commands whenever WordPress trunk updates, but that's annoying and a hassle. I'd love to set this up as a cron job, but I can't seem to figure out how!

I've written a script called wordpress.sh and placed it in the /etc/ folder on the server:

#!/bin/bash
cd /var/www/git/wordpress
git checkout wordpress
git svn rebase
git gc
git push origin wordpress

The script runs just fine. I can SSH into my server, log in as root, and type . /etc/wordpress.sh and everything works just fine. But how can I set this up so that cron does things for me?

I've tried manually adding it to crontab (unsuccessfully), using the Webadmin interface in CentOS to add it as an hourly script (unsuccessfully), and now I'm at a loss! My system logs claim that it's firing (log below):

Feb  4 07:00:01 ---- crond[27745]: (root) CMD (/etc/wordpress.sh    #WordPress mirror)
Feb  4 07:01:01 ---- crond[27773]: (root) CMD (run-parts /etc/cron.hourly)
Feb  4 08:00:01 ---- crond[29095]: (root) CMD (/etc/wordpress.sh    #WordPress mirror)
Feb  4 08:01:01 ---- crond[29142]: (root) CMD (run-parts /etc/cron.hourly)
Feb  4 09:00:01 ---- crond[29708]: (root) CMD (/etc/wordpress.sh    #WordPress mirror)
Feb  4 09:01:01 ---- crond[29718]: (root) CMD (run-parts /etc/cron.hourly)
Feb  4 10:00:01 ---- crond[30328]: (root) CMD (/etc/wordpress.sh    #WordPress mirror)
Feb  4 10:01:01 ---- crond[30337]: (root) CMD (run-parts /etc/cron.hourly)

But nothing actually happens unless I log in and run the script manually. Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个 2024-10-23 01:41:27

Cron 在登录 shell 以外的环境中运行,因此可能的错误是 git 不在路径中。最简单的方法通常是使用 crontab 中每个命令的完整路径(/usr/bin/git 或安装 git 的任何位置)。

cron 运行的脚本中的错误通常会通过邮件发送给用户,因此请尝试查看 root 的邮箱。

Cron runs with another environment than a login shell so a likely error is that git is not in the path. The easiest is usually to use the full path of every command in a crontab (/usr/bin/git or wherever your git is installed).

Errors in scripts run by cron is usually mailed to the user, so try to look in root's mailbox.

梦魇绽荼蘼 2024-10-23 01:41:27

您是否以 root 身份运行您的网站?您是否以 root 身份手动运行脚本?我希望你不要。

现在查看 cron 输出:它从 root 帐户运行您的脚本,并且可能无法找到存储库。将其添加到您帐户的 crontab,而不是 root 的。

Do you run your sites as root? Do you manually run the script as root? I hope you don't.

Now look at cron output: it runs your script from root account, and it probably fails to find the repository. Add it to your account's crontab, not root's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文