NOTE: Change every instance of $USER
in the commands and the Bash file to the user you want this to run for!
-
Save the Script: Save the script above as
auto-rotate.sh
in your home directory (or another location of your choice). -
Make It Executable: Open a terminal (or via SSH) and run:
chmod +x ~/auto-rotate.sh
- Test the Script Manually: To verify it works correctly (while logged in locally on the display), run:
~/auto-rotate.sh
You should see output confirming the display rotation and touchscreen calibration. Adjust the desired_rotation
variable if needed.
- Create a Systemd User Service: Create a systemd service file in your user configuration directory. Run these commands via SSH or a terminal:
mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/rotate.service
Paste in the contents of rotate.service
. Save and exit the editor (in nano, press Ctrl+O, Enter, then Ctrl+X).
- Enable and Start the Service: Reload your user systemd configuration and enable the service:
systemctl --user daemon-reload
systemctl --user enable rotate.service
- Reboot and Verify: Reboot your Raspberry Pi:
sudo reboot
When you log in locally, the systemd user service should run your script after the graphical target is reached, ensuring the proper rotation and touchscreen calibration is applied to your desktop.
- The script uses a 10‑second delay (sleep 10) to ensure the X server is fully ready before running the commands. If needed, you can adjust this value.
- The script automatically detects your primary display and the first touchscreen device it finds. If you have more than one touchscreen or special configuration requirements, you might need to adjust the detection logic.
- Make sure your user’s
.Xauthority
file is valid and accessible; otherwise, the script may not be able to communicate with the X server.