1

I'm trying to write a simple cron job that will keep my org-mode files in sync with a GitHub repo by committing every now and then. For some reason, it always manages to commit the files, but even though I see output for the git push when running the script from the command line, like

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 still tells me I need to push:

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

Here's my script:

#!/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 git@github.com:my/repo.git master

And here's what it looks like in my crontab:

* * * * * echo ["$(date)"] "$(echo "$(~/cron/scripts/org-commit.sh | tail -1)" | xargs)" >> ~/cron/log/org-commit.log 2>&1

In ~/cron/log/org-commit.log, I see this:

[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(+)

Where the first line was when it run without changes, and the second there was one change. It seems as if git push is never even running, as that log line is from git commit...


How can I find what's causing this?

  • It would be much easier to debug if you move the logging into the script and reduce the crontab task line as far as possible. What is that xargs doing there, for example? And the tail -1? – Tomasz Jun 12 at 22:32
  • What does git remote -vv show? I'm guessing your URL for origin doesn't exactly match git@github.com:my/repo.git, so until the next git fetch Git won't know that your upstream was updated... Try using git push origin master instead on your shell script. (Happy to convert this to an answer if it does indeed solve your problem.) – filbranden Jun 12 at 22:56
  • @filbranden git remote -vv shows the same remote I had on the script: origin git@github.com:brunobely/org-mode.git (fetch) origin git@github.com:brunobely/org-mode.git (push) changing the git push line to git push origin master now made it so that running that whole cron task from the command line gets the repo to be pushed, however it's still left unpushed if cron runs it... – Bruno Ely Jun 12 at 23:27
  • @filbranden I believe it might be working now with just git push instead of the other ones, although I'm unsure as to why – Bruno Ely Jun 12 at 23:28
  • @Tomasz I'll try to move that into the script. xargs is just trimming the whitespace around the output I get from tail -1 (I wanted to add to add the last line of output to the commit message) – Bruno Ely Jun 12 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.