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?
tail -1
?git remote -vv
show? I'm guessing your URL fororigin
doesn't exactly matchgit@github.com:my/repo.git
, so until the nextgit fetch
Git won't know that your upstream was updated... Try usinggit push origin master
instead on your shell script. (Happy to convert this to an answer if it does indeed solve your problem.)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 thegit push
line togit 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...git push
instead of the other ones, although I'm unsure as to whyxargs
is just trimming the whitespace around the output I get fromtail -1
(I wanted to add to add the last line of output to the commit message)