Home > Back-end >  git produces no output when called from script via cron (log/shortlog)
git produces no output when called from script via cron (log/shortlog)

Time:07-01

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!

CodePudding user response:

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.)

  • Related