0

I have a repository for storing all my configs (e. g. .ssh/config). From time to time I need a backup them, so I need to run these commands

git commit -am 'Auto git backup `date -I`'
git push origin master

It is pretty annoying so I decided to make a cron job, which runs simple script with these lines above. I ran crontab -e and added new line at the end of file. I also replaced git with which git inside of script. git commit command works perfectly in contrast with git push. When I made some debug prints I got

ssh: Could not resolve hostname bitbucket.org: Name or service not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

But when I run script manually, it works. I think it is because of SSH aliases in my .ssh/config, which looks like

Host     bitbucket.org-personal
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

Host    bitbucket.org-work
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/another_id_rsa

Does somebody have idea, how to propagate SSH aliases to environment, which runs cron jobs? Also maybe I am wrong and problem is caused by something else.

32cupo
  • 850
  • 5
  • 18
  • 36
  • 1
    You miss how the line you wrote into the crontab look like. Are you sure the backup is run when the network is up? – Jakuje Aug 13 '17 at 20:37
  • Line in crontab is `@reboot /home/user/scripts/backup-and-commit.sh` and I added some debug messages and I'm sure cron job works as I want. Do you think it doesn't push commits because of network issues? – 32cupo Aug 14 '17 at 06:17
  • 1
    Yes. The error is obvious that you do not have network. Try to postpone the run some minutes to wait for the network. – Jakuje Aug 14 '17 at 06:23

1 Answers1

0

The issue was a network connection. When I added sleep (or modified solution from here How to check internet access using bash script in linux?) at the begging of the script, it works perfectly.

32cupo
  • 850
  • 5
  • 18
  • 36