Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Created February 9, 2020 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hashbrown777/6e1c6b4f9191788d0f794438f5c49bbd to your computer and use it in GitHub Desktop.
Save Hashbrown777/6e1c6b4f9191788d0f794438f5c49bbd to your computer and use it in GitHub Desktop.
outputs "Locked" & "Unlocked" + `date` every time the [fedora+plasma] machine's screen is locked/unlocked. Add to startup script like `nohup lockMonitor.sh >>locklog.txt &`
#!/bin/bash
#prints out, among other things;
# string "org.kde.screensaver"
#transform it to 'org.kde.screensaver'
service=$(\
dbus-send \
--session \
--dest=org.freedesktop.DBus \
--type=method_call \
--print-reply \
/org/freedesktop/DBus org.freedesktop.DBus.ListNames \
| grep -o '[^"]*.screensaver'
)
#prints out, among other things;
#method bool org.freedesktop.ScreenSaver.SetActive(bool e)
#transform it to 'org.freedesktop.ScreenSaver'
interface=$(
qdbus \
$service /ScreenSaver \
| grep -oP '[^ ]*(?=.SetActive)'
)
path='/ScreenSaver'
#monitor it with a while loop
dbus-monitor "type='signal',interface='$interface',member='ActiveChanged',path='$path'" \
| while read -r line; do
#ignore the metadata and pull the 'boolean <true/false>' line
read line
#check if it is set to true
if echo $line | grep -q 'true'; then
echo "Locked at $(date)"
else
echo "Unlocked at $(date)"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment