【问题标题】:Cron job Auto-Push to Git IssueCron 作业自动推送到 Git 问题
【发布时间】:2016-10-13 22:02:27
【问题描述】:

我有一个 cron 作业设置来运行 bash 脚本以每晚推送到 Git。

cron 作业设置为 root,我已通过:git config credential.helper store 设置了我的 git 凭据:Git push: username, password, how to avoid?(第二个答案)

bash 脚本的代码非常简单

#!/bin/bash

# Nightly push to Bitbucket

# Set some variables
DAY=$(date +%F);

# Make sure we run as root
if [ "$(whoami)" != "root" ]; then
    echo "Only root can do this.";
    exit 1;
else
    # Make sure we are in the right directory
    cd /hosting;
    # Now add any changes
    git add .;
    # Now commit
    git commit -m "$DAY Nightly";
    git push all;
fi;

只要我登录到服务器并以 root 身份运行它,它就可以毫无问题地运行。

但是,它并没有在指定的时间运行。

Crontab -e 设置为:30 3 * * * back-to-git >/dev/null 2>&1

我该怎么做才能让它工作?

【问题讨论】:

  • 什么是cronjob配置?在这里分享一下,看看那里有没有问题。另外,检查debugging crontab
  • 更新了问题以包含该工作,我稍后会查看该链接
  • 这可能是你如何调用你的脚本的问题:back-to-git 单独是 cron 找不到的东西。它是您主目录中的脚本吗?然后确保编写完整路径以及执行它的二进制文件 --> /bin/bash /home/your_user/back-to-git
  • 这是/usr/bin中的脚本也有执行权限
  • 尝试在脚本开头添加(date; whoami) > /tmp/cron-log.txt 之类的内容,并在脚本失败后检查/tmp/cron-log.txt 的内容。如果文件在那里,则作业确实触发了。如果没有,你有一个 cron 问题(不是 Git 问题)。

标签: git bash cron bitbucket


【解决方案1】:

首先,最好将脚本的完整路径包含在 cron 选项卡中。

30 3 * * * /opt/btg/back-to-git >/dev/null 2>&1

/opt/btg/back-to-git 替换为脚本的绝对路径。 然后尽量不要丢弃包含错误的输出:

30 3 * * * /opt/btg/back-to-git &>/home/your_user/btg.log 

这样您可以在 cron 处理脚本时检测错误。

您的最后一条评论表明 bitbucket 可能存在身份验证问题。为了确保您以正确的方式推送,我建议始终在脚本中写出 git 的完整目标。所以尝试使用

git push --all origin 而不是git push all;

当推送到 bitbucket 时,请检查您的 git 设置以使用 ssh 而不是 https

您还可以在 /root/.ssh/config 中指定要使用的主机和 ssh 密钥

Host bitbucket
  HostName bitbucket.org
  User YOUR_USERNAME
  IdentityFile /root/.ssh/id_rsa
  IdentitiesOnly yes

【讨论】:

  • 我使用git push all,因为我同时推送到 github 和 bitbucket。,也尝试使用 ssh...虽然 github 似乎可以工作,但 bitbucket 会引发权限错误。我会将输出(我在上次测试中删除它)重定向到日志并查看
  • 我开始越来越多地认为它是运行作业的用户。在日志中,我又收到了warning: push.default is unset; 消息,然后它显示了Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 消息。两个存储库都存在,都将我的 id_rsa.pub 添加到其中。
  • 我认为您在这里将我推向了正确的方向。看起来我的问题源于 bitbucket 端。仅在 github 上使用 ssh 方法,效果很好。尽管日志确实显示 warning: push.default is unset; 对于它,但提交被推送。我得看看 bitbucket 是否有办法添加write 访问密钥...
  • 如果我能找到更多关于它的信息,并且可以补充,我会把你的答案保持为未回答......但你肯定让我朝着正确的方向前进:),所以你我赢得了赏金;)
  • 很高兴我能帮助您并推动您朝着正确的方向解决您的需求 :)
猜你喜欢
  • 2019-04-05
  • 1970-01-01
  • 2016-09-14
  • 2012-02-26
  • 2012-08-01
  • 2011-08-18
  • 1970-01-01
  • 2012-09-13
  • 2023-04-07
相关资源
最近更新 更多