4

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?

Community
  • 1
  • 1
Kevin
  • 2,684
  • 6
  • 35
  • 64
  • what is the cronjob configuration? Share it here to see if there is something wrong there. Also, check the [debugging crontab](http://stackoverflow.com/tags/crontab/info). – fedorqui Jun 13 '16 at 14:02
  • updated the question to include the job, I'll look at that link in a bit – Kevin Jun 13 '16 at 14:57
  • It is probably a problem on how you call your script: `back-to-git` alone is something cron cannot find. Is it a script in your home directory? Then make sure you write the full path, together with the binary that executes it --> `/bin/bash /home/your_user/back-to-git` – fedorqui Jun 13 '16 at 15:03
  • it's a script in `/usr/bin` also has execute permissions – Kevin Jun 13 '16 at 15:30
  • Still, maybe it is not in the PATH from crontab, so it's best to use the full path. – fedorqui Jun 13 '16 at 17:02
  • I won't know for sure until it fires off again. But, I have other jobs that run in a similar fashion, that all work – Kevin Jun 13 '16 at 17:08
  • Then good luck : ) I gave different hints that may help, now it is your choice to use them or not. – fedorqui Jun 14 '16 at 06:25
  • So... I did try them, and it is still not working. – Kevin Jun 15 '16 at 11:49
  • Cron looks like this: http://prntscr.com/bgnbv0 and the only one that does not work, is this one. – Kevin Jun 15 '16 at 11:50
  • 1
    Try adding something like `(date; whoami) > /tmp/cron-log.txt` at the beginning of the script, and check for the content of `/tmp/cron-log.txt` after the script is supposed to fail. If the file is there, the job did trigger. If not, you have a cron issue (not a Git one). – Matthieu Moy Jun 22 '16 at 06:34
  • Added. I reset the schedule, and will let you know soon – Kevin Jun 22 '16 at 12:50
  • Ok. The cron-log.txt exists :) Least that's a good sign. The code in the question runs when I run it directly form shell. it is moved to /usr/bin/ as root. One more test, without the null dump – Kevin Jun 22 '16 at 13:10
  • Ok... this has me a tad concerned: `fatal: could not read Password for 'https://kpirnie@bitbucket.org': No such device or address` When I run it through shell sudo'd I dont need to login. (I took the steps to ensure that: http://prntscr.com/bjk7vs from http://stackoverflow.com/questions/8588768/git-push-username-password-how-to-avoid?lq=1) – Kevin Jun 22 '16 at 13:22
  • Ok, I tried what is listed as the answer in that question now as well. Seems git+ssh is attempting to connect via the port I changed my shell to. – Kevin Jun 22 '16 at 13:43

1 Answers1

3

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
cb0
  • 8,415
  • 9
  • 52
  • 80
  • I use `git push all` because I push to both github and bitbucket., also tried with ssh... while github seems to work, bitbucket throws a permissions error. I'll redirect the output (i did remove it for my last test), to a log and see – Kevin Jun 22 '16 at 18:29
  • I'm starting to think more and more that it's the user the job is running under. In the log, I am back to getting the ol' `warning: push.default is unset;` message, and then it shows the `Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. ` message. Both repos exist, both have my id_rsa.pub added to the them. – Kevin Jun 22 '16 at 18:50
  • I think you are pushing me in the right direction here. It looks like my problems are stemming on the bitbucket end. using the ssh method with github only, works just fine. tho the log does show that `warning: push.default is unset;` for it, the commit gets pushed. I'll have to see if bitbucket's got a way to add a `write` access key... – Kevin Jun 22 '16 at 20:49
  • In case I can find out more about that, and can add to this, i'll keep your answer as unanswered... but you definately have me heading the right way :), so you've earned the bounty ;) – Kevin Jun 22 '16 at 20:50
  • Bingo! there's apparently 2 spots on Bitbucket that you can configure the ssh key. one is in your account to add a key, the other is in the repo itself. The one in the repo itselfy is read-only. Thanks for helping mate :) – Kevin Jun 22 '16 at 21:55
  • 2
    Glad I could help and push you to the right direction to solve your need :) – cb0 Jun 23 '16 at 07:38