0

On a raspberry with user piuser I have a git repo ~/myfiles/gitrepo, accessing git via ssh, which works just fine with git pull when done manually.

However I want to do this automatically via a cron job, for which i use

* * * * * cd ~/myfiles/gitrepo/ && git pull

the logs say classic git no permission:

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

how can I get this to work?

what i tried so far:

  • check replies from Git auto-pull using cronjob. this resulted in directory not a git repo although it for sure is
  • make sure cron uses piuser by verifying that * * * * * echo "$(date): $(whoami)" >> ~/mylog.log shows piuser in the logs
  • manually doing git pull as piuser works just fine

Thanks for any Ideas!

Dr-Nuke
  • 368
  • 3
  • 11
  • What is the output of `git remote -v`? Maybe your method of accessing the remote repository requires environment variables which are not set by default when running in a cron job. – SebDieBln Aug 04 '23 at 16:35
  • 1
    Is your SSH key encrypted? Does it require a passphrase to decrypt? – Nick ODell Aug 04 '23 at 16:38
  • @nick good point, but the ssh key has no password (for exactly that reason) @Seb ```git remote -v``` shows ```origin git@github.com:.git (fetch) origin git@github.com:.git (push)``` i am not aware on any required environment variables, but I also have not yet found out how to verify – Dr-Nuke Aug 04 '23 at 20:19
  • @Dr-Nuke You can use `env -i bash` to start a shell session with most environment variables removed. You also can run `env` in crontab to see what environment variables it has. – Nick ODell Aug 04 '23 at 20:23

1 Answers1

0

90% of time this is because of wither:

  1. the user who is executing this does not have access to SSH key you are using
  2. The SSH keys is missing or incorrect pair
  3. The permissions on the SSH key is not as expected.
  • They said that the user normally has the necessary permissions. – mkrieger1 Aug 04 '23 at 16:39
  • good points. 2) can be ruled out, as manually pushing/ pulling works as intended. For 1) I could verify that the cron job is run as the intended user who ought have access. I wonder if the cron job still has problems using the ssh key, or in conjunction with the ssh-agent. for 3), I checked that pushing changes to remote does not require enterin a passphrase – Dr-Nuke Aug 04 '23 at 20:23
  • @Dr-Nuke I am strongly suspecting this to be point 1. If you add `ssh-add -L` to the beginning of the crontab command, does it print the expected key? I suspect it does not, and would then instead of specifying the `cd` and `pull` commands in the crontab put those into a script file where you add `eval $(ssh-agent)` and `ssh-add /your/ssh/keyfile` at the beginning. – hlovdal Aug 05 '23 at 06:46