Git commit doesn't work from cron job, although git pull does the job

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 click:

 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 you can see from the log file, β€œcommit” does nothing while β€œpull” is working fine:

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

I executed the same command, a minute later, from the command line, and received the following log, which means that the commit occurred:

 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 when committing from cron job?

NOTE. I also did the same experiment with the actual change in the test file. I found out that, indeed, the commit did not come from crontab (nothing was pressed upstream), but came from the command line.

+7
git crontab
source share
1 answer

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

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

I.e:

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

And add the shebang directive to the bash script a:

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

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

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

After changing the redirection order, as recommended, my log file does show a long error message:

 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 default identity 

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

The cron task will most likely be run with a different account (or root): its global git configuration ( ~/.gitconfig ) will not be the same as the one you installed in your user account.

One simple solution is to repeat these git config (without -global) inside the target git repository : this will register the user id in repo, rather than relying on a global configuration that is not shared between accounts.

+7
source share

All Articles