1

I used the following script (autoupdate.sh) to update my git repository automatically using SSH whenever I make a change to the local repository in raspberry pi 3B.

#!/usr/bin/env bash

DATADIR="/home/pi/data"
cd $DATADIR

if [[ -n $(git status -s) ]]; then
    echo "Changed found. Pushing changes..."
    git add -A && git commit -m "$1: Update files" && git push origin main
else
    echo "No changes found. Skip pushing."
fi

Then I call a script measurement.sh that calls the above script whenever the internet is connected ( I used 4G dongle USB). Something like

...    
cd ~/data; bash autoupdate.sh $DATE
...

However, when I run sudo bash measurement.sh it encountered the errors (It has made a commit but not push). Without sudo it works fine.

Permission denied(public key)
...

I checked from GitHub document https://docs.github.com/en/github/authenticating-to-github/troubleshooting-ssh/error-permission-denied-publickey by regenerating the ssh key as well as verified the public key but it did not solve at all. When I pushed commits in a separate terminal it works fines so I do not think the issue relates to the SSH key. I doubt that to run the script successfully it with sudo, the SSH keygen must also be generated with sudo at first.

What could be the reasons for it?

  • `sudo`, damn it! – phd Jul 17 '21 at 22:42
  • 1
    See https://stackoverflow.com/a/4592122/7976758 and https://stackoverflow.com/a/34917354/7976758 Found in https://stackoverflow.com/questions/tagged/git+ssh+sudo – phd Jul 17 '21 at 22:53

1 Answers1

0

Without sudo it works fine.

So why use sudo in the first place.
As commented, using sudo alone means using commands as root.
At least, a sudo -u <auser> would means the ~ in cd ~/data would be resolved by the appropriate /home/auser, instead of /root.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • hello since I need to write to some log files that its owner is root. However, I can change the owner of those log files to current user now it can be written. – CoffeePlease Jul 18 '21 at 13:05
  • @CoffeePlease Changing the ownership would be easier indeed. – VonC Jul 18 '21 at 13:31