3

I'm trying to commit/push changes to a remote Git server with the below sample code:

#!/bin/sh

USER='username'
REPO='/home/'${USER}'/Sites/git/bitbucket/kb'
COMMIT_TIMESTAMP=`date +'%Y-%m-%d %H:%M:%S %Z'`
DATELOG=`date +'%Y-%m-%d-%H-%M-%S'`
LOG="/tmp/${DATELOG}.txt"

MKDOCS=`which mkdocs`
GIT=`which git`
NOTIFY=`which notify-send`

# Only proceed if we have a valid repo.
if [ ! -d ${REPO}/.git ]; then
  echo "${REPO} is not a valid git repo! Aborting..." >> ${LOG}
  exit 0
else
  echo "${REPO} is a valid git repo! Proceeding..." >> ${LOG}
fi

cd ${REPO}
${MKDOCS} build --clean >> ${LOG}
${GIT} add --all . >> ${LOG}
${GIT} commit -m "Automated commit on ${COMMIT_TIMESTAMP}" >> ${LOG}
${GIT} push [email protected]:username/repo.git master >> ${LOG}

# Depends on libnotify
${NOTIFY} 'KB notification' 'Changes were pushed to Bitbucket.' --icon=dialog-information >> ${LOG}

If I invoke the shell script manually (e.g. ./commit.sh) it works immediately. On the contrary, when triggered via a cron job everything works just fine until the git push which then never seems to be triggered for some odd reason.

Here's my crontab line:

*/20 * * * * /home/username/Sites/git/repo/commit.sh

And some verbosity for git push

09:53:32.732216 git.c:349               trace: built-in: git 'push' '[email protected]:username/repo.git' 'master'
09:53:32.732514 run-command.c:341       trace: run_command: 'ssh' '[email protected]' 'git-receive-pack '\''username/repo.git'\'''
09:53:39.665197 run-command.c:341       trace: run_command: 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
09:53:39.665526 exec_cmd.c:134          trace: exec: 'git' 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
09:53:39.666778 git.c:349               trace: built-in: git 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 4.23 KiB | 0 bytes/s, done.
Total 7 (delta 4), reused 0 (delta 0)
To [email protected]:username/repo.git
   0ef4905..91437d0  master -> master

How come git push would be triggered only when the script is invoked manually and not when run via the crontab?

4
  • 1
    Did cron send you any e-mail? Commented Aug 31, 2015 at 20:30
  • It could rely on environment variables like $HOME or $USER.
    – myaut
    Commented Aug 31, 2015 at 20:30
  • cron is not the culprit it seems as the cron job is executed and logged just fine under /var/log/syslog e.g. Aug 31 22:44:01 laptop CRON[9364]: (username) CMD (/home/username/Sites/git/repo/commit.sh)
    – anavarre
    Commented Aug 31, 2015 at 20:47
  • Did you find out any solution? I have the same problem. (btw, I don't know where to look for email sent by deamon. Do I have to do anything to receive it? In Mac, I receive it automatically in a folder, but not on Ubuntu) Commented May 31, 2016 at 13:10

2 Answers 2

6

What director(ies|y) are you looking to do this in? It is not clear from you script. Try going into the directory1, something like:

#!/bin/sh

GIT=`which git`
REPO_DIR=/home/username/Sites/git/repo/
cd ${REPO_DIR}
${GIT} add --all .
${GIT} commit -m "Test commit"
${GIT} push [email protected]:username/repo.git master

Or you can do a git add --all /path/to/git/repo/files.

Edit:

Running a script from the command line is not necessarily the same as running it from cron; see this Q&A: What is the 'working directory' when cron executes a job, especially Gilles' answer.

1Some ideas on how to check if a command succeeded

15
  • That's going to be hilarious when the unchecked cd call fails. set -e or check the result of the cd.
    – thrig
    Commented Aug 31, 2015 at 20:38
  • Just a suggestion to do cd; checking is left to the user (-: Great suggestion nonetheless!
    – KM.
    Commented Aug 31, 2015 at 20:39
  • That's the reason I think. I asked OP to check any mail sent by cron daemon for him to make sure. However, there are 2 problems with this script: 1. what if there is no git installed? 2. what if cd fails? Commented Aug 31, 2015 at 20:40
  • 1
    Not to mention the issues with the use of which, unix.stackexchange.com/q/85249/4252
    – KM.
    Commented Aug 31, 2015 at 20:47
  • 1
    Might be time for sanity checks. Ensure keys are in place, with proper permissions, and they match those on bitbucket. Also, try GIT_CURL_VERBOSE=1 GIT_TRACE=1 git push [email protected]:username/repo.git master and see/post what you get.
    – KM.
    Commented Sep 1, 2015 at 13:26
0

I faced the same problem and that is how I fixed it:

  1. First, try to find out why what is the error of your push. You can get the error log by adding 2>&1 at the end of your script. In your case it would be:

    ${GIT} push [email protected]:username/repo.git master >> ${LOG} 2>&1
    
  2. Several reason can cause your push is not working from within your shell. Check these criteria:

    • your user name and password is Stored/Cached correctly. You can push to git URL directly including your User/Pass just for test (DON'T forget to remove them). e.g.:

      ${GIT} push --repo https://YOUR_USER_NAME:[email protected]/repo.git  
      
    • Make sure Git is not complaining about SSL (You can temporarily ignore it by adding this command before your push in your script:

      export GIT_SSL_NO_VERIFY=1
      
  3. In case you are not if git push is executed in the right directory, you can go to your desired directory before your script is executed by adding pre-step to your cron job as follow:

    */20 * * * * cd /home/'${USER}'/Sites/git/bitbucket/kb && /home/username/Sites/git/repo/commit.sh
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .