Failed to execute git with crontab job

I am trying to do git commit

with a bash script. I have set up a cron job to periodically execute this script. Everything works as expected if I execute the script directly. For some reason, when the script is called from crontab

, it 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 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 cannot execute git commit

and git push

that works when the script is run directly.

Here is the entry crontab

.

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

      

I'm pretty sure both crontab and script are executed in bash. I am using CentOS 5.11.

Any pointers would be appreciated.

-Thank,

Shamik

************ CONNECTED SOLUTION **************

Based on @CholNhial and @Marc crontab

, the full git path is required to execute the command. I 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 ..

      

+3


source to share


1 answer


As you can see from the results, the problem is that when your script is running, it cannot find your command git

. You must add the full path to all your commands.

When a command is given to the bash command, it first looks for built-in commands and then starts checking the command in $PATH

. A common and simple mistake is to assume that if your script is run when you run it directly from your shell, it will work everywhere, but it is not as you see here. The reason is that when you enter a login shell, you acquire many add-ons as part of your environment. In particular, .bash_login

it is usually performed in login shells. This makes the execution of commands more convenient, but it is a luxury that your task will not take care of.

As with any programming, it is best not to add any additional assumptions about your environment. If this is something your script depends on, it is best to state it explicitly. This includes full paths to any team.



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

which git

      

This should give you the full path, which you can then add to your script. Then just replace git

with /your/full/path/to/git

in your script.

0


source







All Articles