Ubuntu – How to run a script at screen lock / unlocks in ubuntu 17.10


My problem is the same of this question but I am not able to get it working in ubuntu 17.10:

How to run a command or script at screen lock/unlock?

I want to run a script that changes my keyboard RGB configuration at screen locks. The script runs flawless. The problem is to get the event of the locking/unlocking. I have try using dbus-monitor as said in that question and as said here:


So running this script:


dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | \
( while true
    do read X
    if echo $X | grep "boolean true" &> /dev/null; then
        echo "locking at $(date)" >> $HOME/time_xprofile
    elif echo $X | grep "boolean false" &> /dev/null; then
        echo "unlocking at $(date)" >> $HOME/time_xprofile
    done )

But it only works… a few times(!)…. I cannot understand what happens.

I'm using ubuntu 17.10 with ubuntu's gnome over X-server (no Wayland) and have try vanilla gnome and have the same problem.

I have try too:

dbus-monitor > out.log

To see ALL the traces that occur while locking/unlocking and it doesn't appear that signal… Only…. well… very few times…

I don't know what to do know, Any advice will be helpfull.

Best Answer

Assuming you're using Gnome then Nowadays I think it's better to listen to the LockedHint rather than screensaver messages. That way you're not tied to a screensaver implementation.

Here's a simple script to do that:

gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint

Gives this:

/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'LockedHint': <true>}, @as [])
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'LockedHint': <false>}, @as [])