当通过cron从脚本调用时,git不会产生输出

发布于 2025-02-11 16:45:16 字数 957 浏览 1 评论 0原文

我有一个脚本git-Test转到本地git repo并调用git Shortlog以跟踪最近的更改。为了进行测试目的,我只使用了以下行:

#!/bin/bash
cd /myrepo.git
result="$(/usr/bin/git shortlog -3)"
echo "$(date):$result" >> /tmp/git-log

crontab条目是

* * * * * /home/username/git-test >> /tmp/cron-output 2>&1

在所有脚本工作之后都从控制台从Cron开始使用的,可以执行简单的ls -lh而不是git命令。但是它没有用git命令从cron呼叫。因此问题必须是git。

我尝试了:

  1. 从终端调用脚本 - >做得很好,
  2. 从上面的 - >在/tmp/git-log中没有git输出,也没有/tmp/cron-o-Output中的
  3. 错误)“ 而不是$(/usr/bin git shortlog -3) - >做
  4. cron的良好调用脚本,直接用git shortlog -3&gt直接写入文件>/tmp/git-log - > /tmp/git-log
  5. 将git cd /myrepo.git; git shortlog -3>>> /tmp/cron-ofput

可能是什么?

任何想法

I've got a script git-test going to a local git repo and calling git shortlog to keep track of recent changes. For testing purposes, I just used these lines:

#!/bin/bash
cd /myrepo.git
result="$(/usr/bin/git shortlog -3)"
echo "$(date):$result" >> /tmp/git-log

the crontab entry is

* * * * * /home/username/git-test >> /tmp/cron-output 2>&1

After all the script IS WORKING called from console and IS WORKING from cron with doing a simple ls -lh instead of the git command. But it IS NOT WORKING called from cron with the git command. So the problem must be git.

I tried:

  1. calling script from terminal -> doing fine, getting git short logs
  2. calling script from cron, like above -> no git output in /tmp/git-log and no errors in /tmp/cron-output
  3. calling script from cron, but doing a $(ls -lh)" instead of $(/usr/bin git shortlog -3) -> doing fine
  4. calling script from cron, writing to a file directly with git shortlog -3 >> /tmp/git-log -> no git output in /tmp/git-log and no errors in /tmp/cron-output
  5. putting the git cd /myrepo.git;git shortlog -3 >> /tmp/cron-output 2>&1 directly in crontab -> no output and no errors in /tmp/cron-output

Any Ideas what the problem might be?

Thank you!

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

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

发布评论

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

评论(1

jJeQQOZ5 2025-02-18 16:45:16

Git Shortlog具有怪异的“功能”。 它已记录为 ,但是“功能” 感到惊讶的是,它经常被错过:

如果在命令行和任何标准上没有修订
输入不是终端,也没有当前的分支,git招身者
将从标准输入中输出日志读取的摘要,而无需
引用当前存储库。

您从crontab运行的stdin已将其重定向到文件(嗯,/dev/dev/null,但“不是终端”),因此您可以点击此功能:git Shortlog读取从stdin登录,这是空的,因此其输出为空。

治愈方法是为其提供适当的git log -pretty =简短输出,如文档中的概述行所示。

(为什么“功能”?我不知道。我根本不了解这种行为。)

git shortlog has a weird "feature". It is documented, right up front in the manual page, but the "feature" is so surprising that it is often missed:

If no revisions are passed on the command line and either standard
input is not a terminal or there is no current branch, git shortlog
will output a summary of the log read from standard input, without
reference to the current repository.

Your run from crontab has stdin redirected to a file (well, to /dev/null, but "not a terminal") so you hit this feature: git shortlog reads the log from stdin, and that's empty, so its output is empty.

The cure is to feed it the appropriate git log --pretty=short output, as seen in the SYNOPSIS line in the documentation.

(Why the "feature"? I have no idea. I don't understand this behavior at all.)

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