Skip to main content
3 of 4
made more readable; removed unecessary subshell and unecessary grep calls; made immediately executable
Petr Skocik
  • 29.7k
  • 18
  • 90
  • 154

gnome-screensaver emits some signals on dbus when something happens.

Here the documentation (with some examples).

You could write a scripts that runs:

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"

and that does what you need anytime dbus-monitor prints a line about the screen locked/unlocked.


Here a bash command to do what you need:

dbus-monitor --session "type='signal',interface='org.mate.ScreenSaver'" |
  while read x; do
    case "$x" in 
      *"boolean true"*) echo SCREEN_LOCKED;;
      *"boolean false"*) echo SCREEN_UNLOCKED;;  
    esac
  done

Just replace echo SCREEN_LOCKED and echo SCREEN_UNLOCKED with what you need.

peoro
  • 4k
  • 3
  • 35
  • 32