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.

  • If you run in a terminal dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'", does it toggle true/false on lock/unlock? Nov 8, 2017 at 9:17
  • I tryed it before running dbus-monitor withouth parameters and no, it doesn't toggle true/false. Nov 8, 2017 at 9:22
  • Ah, sorry, should have seen it in the question . I asked because it does work here though. (UB 17.10, mutter). Nov 8, 2017 at 9:24
  • Can't test it on your system, but could you first run: pgrep -f gnome-screensaver, and subsequently: sleep 10 && pgrep -f gnome-screensaver, immediately lock screen, wait10-15 seconds and then unlock again. On my system [1] shows one pid, [2] shows two pids. That would be something to work with. If dbus is no option. Nov 8, 2017 at 9:37
  • ^ works perfectly, and should be fine on all gnome-based systems. Nov 8, 2017 at 11:13

2 Answers 2


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 [])
  • 1
    I stole this idea and made it into a reusable script called lockheed. I'm running it within systemd to automate a few nice things when I unlock. You can find the script here: github.com/gogama/lockheed.
    – 0xbe5077ed
    Nov 4, 2022 at 17:45

Use the existence of the process gnome-screensaver-dialog as an alternative trigger

If dbus is not possible or not preferred for whatever reason, you can use the existence of the process gnome-screensaver-dialog as a trigger. The process is called on lock screen.

In a python script:

#!/usr/bin/env python3
import psutil
import time
import subprocess

procname = "gnome-screensaver-dialog"
lock_command = "/path/to/lockscript"
unlock_command = "/path/to/unlockscript"

lock1 = None

while True:
    lock2 = procname in (p.name() for p in psutil.process_iter())
    if lock2 != lock1:
        if lock2:
        lock1 = lock2

How to use

  1. Copy the script into an empty file, save it as locktoggle.py
  2. In the head section:

    lock_command = "/path/to/lockscript"
    unlock_command = "/path/to/unlockscript"

    set the commands or paths to the scripts you'd like to run (leave the double quotes!)

  3. Test- run the script with the command:

    python3 /path/to/locktoggle.py

N.B. In case you only run a command on either one of the events lock/unlock, simply comment out the corresponding line in the section:

if lock2 != lock1:
    if lock2:
        # subprocess.Popen(lock_command)
        # subprocess.Popen(unlock_command)
  • It's a great idea but didn't worked for me :(. There's no gnome-screensaver-dialog. I think that it could be -and is only a theory- that ubuntu doesn't have, by default, screen-saver and it only has a login screen? I'll try at home instaling xscreensaver and using your solution that I found very clean and reusable. Nov 9, 2017 at 13:47
  • @eddieferetro huh?? That is weird, gnome screensaver dialog, although the name suggests so, has nothing to do with screensaver, I don't have screensaver either. Nov 9, 2017 at 13:52
  • @eddieferetro could you run sleep 10 && pgrep -f gnome-screensaver, immediately lock screen, wait10-15 seconds and then unlock again? See what the output is? Your distro is Gnome? (Ubuntu Gnome) Nov 9, 2017 at 13:55
  • i'm cannot access my computer from here (proxies). I did what you say yesterday and didn't show any process number... :( is weird, I know, I'm going crazy with this... My distro is ubuntu 17.10 and the desktop is the default ubuntu-gnome... I'll retry this nigth to avoid the posibility of a human error... but I remember myself doin what you say and thinking on taking a screenshot because I know that you would find it incredible... as I found it... Nov 9, 2017 at 14:04
  • 1
    @eddieferetro I'll install 17.10/Gnome in a VM. Curious to what I will see :) Nov 9, 2017 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .