9

In my crontab, I have the following line:

48 14 * * * bash /home/erelsgl/git/erel-sites/add-commit-push.bash "from home" 2&>1 >> /home/erelsgl/logs/backup_from_home.log

The script does what its name implies - add, commit and push:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
echo --------------------
date
echo == add ==
/usr/bin/git add -A
echo == commit ==
/usr/bin/git commit -m "$1"
echo == pull ==
/usr/bin/git pull
echo == push ==
/usr/bin/git push

As seen in the log file, the "commit" does nothing while the "pull" works fine:

Fri Oct 23 14:48:01 IDT 2015
== add ==
== commit ==
== pull ==
Already up-to-date.
== push ==

I ran the exact same command, a minute later, from the command line, and got the following log, which means that the commit did happen:

Fri Oct 23 14:49:31 IDT 2015
== add ==
== commit ==
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean
== pull ==
Already up-to-date.
== push ==

What is the problem in running commit from a cron job?

NOTE: I also did the same experiment with an actual change in a test file. I found out that, indeed, the commit didn't happen from the crontab (nothing was pushed to upstream) but did happen from the command line.

Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • 3
    `git push` requires a .gitconfig file to set things like the author and email. git will not push without those settings present. It looks like - because youre running it through cron - git cannot locate this file because cron runs in a different environment. You could also try adding `2>&1` to your cron script so you also get stderr output in your log. – Protagonist Oct 23 '15 at 11:25
  • You should add `2>&1` as @Protagonist says , there probably is an error with git commit, either `$1` is empty and git is complaining that the message is empty, either no files were added and git is complaining because the commit is empty – edi9999 Oct 23 '15 at 11:56
  • I added "2>&1". Now, when I run from command line, there is indeed a message saying that there is nothing to commit. But when run from crontab, no message appears (see my edit). It seems that "git commit" does not run at all! why? – Erel Segal-Halevi Oct 23 '15 at 12:07
  • 1
    Which users crontab entry is it? – iltempo Oct 25 '15 at 18:19
  • @iltempo it is my crontab (user erelsgl). – Erel Segal-Halevi Oct 26 '15 at 05:21

1 Answers1

7

cron uses sh by default, so try, as described in "How to redirect output to a file from within cron?":

  • no 'bash'
  • with 2>&1 at the end

That is:

48 14 * * * /home/erelsgl/git/erel-sites/add-commit-push.bash "from home" >> /home/erelsgl/logs/backup_from_home.log 2>&1

And put in your bash script a shebang directive:

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
...

Another form would be to use a bash command line (as in "Redirecting the output of a cron job"):

48 14 * * *  /bin/bash -l -c '/path/to/script >> ./log/my_log.log 2>&1'

After changing the order of redirection as recommended, my log file indeed shows a long error message saying:

Please tell me who you are. 
Run git config --global user.email "you@example.com" 
    git config --global user.name "Your Name" 
to set your account's default identity

I already did this in my account, but the cron job probably does not find the config file. How can I make it find the correct git config file?

The cron job is likely to run with another account (or root): its global git config (~/.gitconfig) won't be the same as the one you set with your user account.

One simple solution is to repeat those git config (without --global) inside the target git repo: that will register the user id in the repo itself, instead of relying on a global config which is not shared across accounts.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Done, but it did not help... (why would $BASH_SOURCE[0] have any effect on git commit?) – Erel Segal-Halevi Oct 25 '15 at 17:57
  • @ErelSegal-Halevi OK. I have rewritten the answer, based on http://unix.stackexchange.com/a/52332/7490 – VonC Oct 25 '15 at 18:06
  • @ErelSegal-Halevi I have edited the answer with another option – VonC Oct 25 '15 at 18:18
  • OK, after changing the order of redirection as recommended, my log file indeed shows a long error message saying: "*** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity.". I already did this in my account, but the cron job probably does not find the config file. How can I make it find the correct git config file? I have the same problem whether I run the script with or without "/bin/bash". – Erel Segal-Halevi Oct 26 '15 at 06:10
  • @ErelSegal-Halevi the cron job is likely to run with another account (or root): its global git config (~/.gitconfig) won't be the same as the one you set with your user account. – VonC Oct 26 '15 at 06:11
  • OK, so how do I tell it to use my .gitconfig? – Erel Segal-Halevi Oct 26 '15 at 06:12
  • @ErelSegal-Halevi I have edited the answer with a proposed solution. – VonC Oct 26 '15 at 06:15