I have a systemd timer service which runs a script every 5 minutes to check if the monitor has been powered off. If it is powered off, it locks the screen.
But I'm having some problems with the users and permissions. It seems the script must be run as sudo to query the monitor's power state, but this causes the xsecurelock to require the root password to unlock the screen. How can I fix this?
I should say: I want to keep this approach and the use of xsecurelock since this is working on my other PCs. Needing sudo to query the power state seems peculiar to this PC.
- Systemd service
Description=Check if Monitor is switched off and run xsecurelock
After=network.target
[Service]
User=dave
Environment=XAUTHORITY=/home/dave/.Xauthority
Environment=DISPLAY=:0
Type=simple
ExecStart=/home/dave/monitor_check.sh --debug
StandardOutput=journal
StandardError=journal
Restart=no
[Install]
WantedBy=multi-user.target
- Script
XSECURELOCK_SAVER=saver_xscreensaver
export XSECURELOCK_SAVER
echo "The value of XSECURELOCK_SAVER is: $XSECURELOCK_SAVER"
state=$(ddcutil getvcp D6 2>&1)
echo "$state"
sleep 2
if [[ "$state" == *"Display not found"* ]]; then
echo "Monitor is off, executing command..."
exec xsecurelock
break
else
echo "Display is found, nothing to do."
fi