我正在尝试编写一个简单的 cron 作业,通过时不时地提交,使我的组织模式文件与 GitHub 存储库保持同步。由于某种原因,它总是设法提交文件,但即使我在git push
从命令行运行脚本时看到输出,例如
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 329 bytes | 329.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:my/repo.git
df98efb..0d4943b master -> master
git status
仍然告诉我我需要推动:
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
这是我的脚本:
#!/bin/sh
message="auto-commit from $USER@$(hostname -s) on $(date)"
GIT=`which git`
REPO_DIR=~/org
cd ${REPO_DIR}
${GIT} add --all .
${GIT} commit -m "$message"
${GIT} push [email protected]:my/repo.git master
这是我的 crontab 中的样子:
* * * * * echo ["$(date)"] "$(echo "$(~/cron/scripts/org-commit.sh | tail -1)" | xargs)" >> ~/cron/log/org-commit.log 2>&1
在 中~/cron/log/org-commit.log
,我看到了这个:
[Wed Jun 12 16:44:00 CDT 2019] nothing to commit, working tree clean
[Wed Jun 12 16:45:00 CDT 2019] 1 file changed, 1 insertion(+)
第一行是在没有更改的情况下运行时的位置,第二行是有一个更改时的位置。似乎从来git push
没有运行过,因为该日志行来自git commit
......
我怎样才能找到造成这种情况的原因?
答案1
我认为你的问题git push
实际上是异步的。将结果分配给变量然后回显结果应该可行。类似以下内容应该输出您期望的响应。
#!/bin/sh
message="auto-commit from $USER@$(hostname -s) on $(date)"
GIT=`which git`
REPO_DIR=~/org
cd ${REPO_DIR}
${GIT} add --all .
${GIT} commit -m "$message"
gitPush=$(${GIT} push -vvv [email protected]:my/repo.git master 2>&1)
echo "$gitPush"