I am trying to get a way to execute a script from a root session, but I am having some trouble with it. I'll lay out the issue:
I have a script that performs some tasks as the regular user 'kolterdyx', and some others as root with sudo. What this script does is rotate a screen and update the touchscreen calibration matrix automatically. I spent about a week troubleshooting that and the script I came up with is as follows:
/home/kolterdyx/.cargo/bin/gnome-randr modify HDMI-1 --rotate "$1"
rule='ATTRS{idVendor}=="27c0", ATTRS{idProduct}=="0818", ENV{LIBINPUT_CALIBRATION_MATRIX}='
right='"0 -1 1 1 0 0"'
left='"0 1 0 -1 0 1"'
inverted='"-1 0 1 0 -1 1"'
normal='"1 0 0 0 1 0"'
case "$1" in
left )
RULE="$rule$left"
;;
right )
RULE="$rule$right"
;;
inverted )
RULE="$rule$inverted"
;;
normal )
RULE="$rule$normal"
* )
echo "You need to pass an argument"
exit
;;
esac
sudo bash -c "echo '$RULE' > /etc/udev/rules.d/99-calibration.rules"
sudo udevadm control --reload-rules
sudo udevadm trigger
sudo bash -c 'echo 1-1 > /sys/bus/usb/drivers/usb/unbind'
sudo bash -c 'echo 1-1 > /sys/bus/usb/drivers/usb/bind'
The system this is running on is Raspberry Pi OS, which I installed without a desktop, and then installed Gnome, which by default uses Wayland and Gnome-Shell. That opened the can of worms that is trying to perform X11 tasks on a non X11 environment, through SSH (because I can't hook up a keyboard to the raspberry pi).
I managed to get that script working, but it only works if I run it as kolterdyx, directly from the user shell. sudo -u kolterdyx or runuser -u kolterdyx as any other user won't work, and the error I get is as follows:
Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
This happens because of the first line, which is a command from gnome-randr-rust. For some reason, if I run it by logging in as kolterdyx it works fine, but if I run it as root either with or without sudo -u kolterdyx, I get the previous error. Isn't sudo supposed to make the target user run the command? What's the difference and how can I get the command to run as my user from the root user?