I have a cron job setup to run a bash script to push to Git every night.
The cron job was setup as root, and I have set my git credentials via: git config credential.helper store
as per: Git push: username, password, how to avoid? (second answer)
The code for the bash script is pretty simple
#!/bin/bash
# Nightly push to Bitbucket
# Set some variables
DAY=$(date +%F);
# Make sure we run as root
if [ "$(whoami)" != "root" ]; then
echo "Only root can do this.";
exit 1;
else
# Make sure we are in the right directory
cd /hosting;
# Now add any changes
git add .;
# Now commit
git commit -m "$DAY Nightly";
git push all;
fi;
And runs without a hiccup so long as I log into the server and run it as root.
However, it does not run at the specified time.
Crontab -e is set with: 30 3 * * * back-to-git >/dev/null 2>&1
What can I do to get it to work?
First off, it's alway better to include the full path of your script inside the cron tab.
30 3 * * * /opt/btg/back-to-git >/dev/null 2>&1
Replace /opt/btg/back-to-git
with the absolute path to your script. Then try to not throw away the output which my contain errors:
30 3 * * * /opt/btg/back-to-git &>/home/your_user/btg.log
This way you can detect errors while cron is processing the script.
Your last comment indicates that there might be auth problems with bitbucket. To make sure you push in the right way I would advise to always write out the full target for git inside scripts. So try to use
git push --all origin
instead of git push all;
The please check your git settings to use ssh
and not https
when pushing to bitbucket.
You can also specify the host and ssh key to use in your /root/.ssh/config
Host bitbucket
HostName bitbucket.org
User YOUR_USERNAME
IdentityFile /root/.ssh/id_rsa
IdentitiesOnly yes
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments