8

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:

https://people.gnome.org/~mccann/gnome-screensaver/docs/gnome-screensaver.html

So running this script:

#!/bin/bash

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
    fi
    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.

derHugo
  • 3,376
  • 5
  • 34
  • 52
eddieferetro
  • 308
  • 1
  • 3
  • 10

2 Answers2

9

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 [])
Matthew
  • 962
3

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:
    time.sleep(2)
    lock2 = procname in (p.name() for p in psutil.process_iter())
    if lock2 != lock1:
        if lock2:
            subprocess.Popen(lock_command)
            print("locked")
        else:
            subprocess.Popen(unlock_command)
            print("unlocked")
        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)
        print("locked")
    else:
        # subprocess.Popen(unlock_command)
        print("unlocked")
Jacob Vlijm
  • 85,475