5

I'm trying to do a git commit using a bash script. I've setup a cron job to execute this script periodically. Everything works as expected if I execute the script directly. For some reason, when the script is invoked from the crontab, the git commit fails. Here's the script :

#!/bin/bash
cd /mnt/ebs2/sitemap
echo "Calling java application to generate sitemap"
java -jar SiteMap-1.0-jar-with-dependencies.jar -i sitemapconfig.xml -o /mnt/ebs2/sitemap/website_sitemaps -url ADSKContentURL
echo "sitemap generation complete.."
cd website_sitemaps
chmod 750 *
echo "Updated file permission, commiting to git..."
git commit -am 'automated weekly update'
git push -u
echo "git commit done..."
cd ..

This is the output from the crontab :

Calling java application to generate sitemap
sitemap generation complete..
Updated file permission, commiting to git...
/mnt/ebs2/sitemap/WeeklyUpdate.sh: line 10: git: command not found
/mnt/ebs2/sitemap/WeeklyUpdate.sh: line 11: git: command not found
git commit done...

As you can see, it fails to execute git commit and git push, which works when the script is run directly.

Here's the crontab entry.

0 2 * * 2 /bin/bash /mnt/ebs2/sitemap/WeeklyUpdate.sh

I'm making sure that both crontab and the script is executed in bash. I'm using CentOS 5.11.

Any pointers will be appreciated.

-Thanks,

Shamik

************ EDITED SOLUTION **************

Based on @CholNhial and @Marc, crontab needs the complete git path to execute the command. I've updated the script to use the full path.

#!/bin/bash
cd /mnt/ebs2/aknsitemap
echo "Calling java application to generate sitemap"
java -jar ADSKSiteMap-1.0-jar-with-dependencies.jar -i sitemapconfig_Elvis.xml -o /mnt/ebs2/aknsitemap/aknwebsite_sitemaps -url ADSKContentURL
echo "sitemap generation complete.."
cd aknwebsite_sitemaps
chmod 750 *
echo "Updated file permission, commiting to git..."
/usr/local/bin/git commit -am 'automated weekly update'
/usr/local/bin/git push -u
echo "git commit done..."
cd ..
galath
  • 5,717
  • 10
  • 29
  • 41
Shamik
  • 1,671
  • 11
  • 36
  • 64
  • I'm going to take a stab and say that if you go to /mnt/ebs2/sitemap/ and try and run the git command it will not work. Have you checked to make sure the git command is in the same path? Try `cd /mnt/ebs2/sitemap/` `command -v git`. Also, just by stating the #!/bin/bash shebang at the top of the shell script, it should run using bash. – Chirality Jul 24 '15 at 02:05
  • 1
    You need the full path to `git`. Find it first using `which git` then edit your script to use something like `/usr/bin/git`. Gentleman how are you going to enter the password though when pushing? I've heard you can specify a file containing the password. – Chol Nhial Jul 24 '15 at 02:06
  • 2
    `cron` doesn't necessarily use the same path as you have a the manual command line. This article might help: http://stackoverflow.com/questions/2388087/how-to-get-cron-to-call-in-the-correct-paths – Marc Jul 24 '15 at 02:56
  • @CholNhial .. thanks guys, the git path was the issue. Once I changed the path to "/usr/local/bin/git commit" , it worked as expected. Appreciate your help. – Shamik Jul 24 '15 at 04:14
  • @Marc .. Thanks, the full path worked. Also, thanks for the link. – Shamik Jul 24 '15 at 04:14
  • 1
    Please post your answers as answers; don't edit them into the question. – melpomene Dec 04 '18 at 02:02

1 Answers1

1

As you can see from the output, the problem here is that when your script is running, it cannot find your git command. You should add the full path to all of your commands.

When bash is given a command, it first looks for built-in commands and then begins checking the for the command in $PATH. It is a common and easy mistake to assume that if your script runs when you run it directly from your shell that it will run the same anywhere, but this is not the case as you see here. The reason is that when you enter a login shell, you acquire a lot of extras as part of your environment. In particular, .bash_login is typically executed on login shells. This causes makes running commands more convenient, but it is a luxury that your cron job will not have.

As with any programming, it is best to not many any additional assumptions about your environment. If it is something that your script depends upon, it is best to state it explicitly. This goes for full paths to any command.

If you need to locate the path for a command, from your shell, you can run something like:

which git

This should give you the full path, which you could then add to your script. Then simply replace git with /your/full/path/to/git in your script.

DKing
  • 574
  • 5
  • 16