Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script I would like to have automatically invoked every time the screen is unlocked. Does Ubuntu provide some support for users who wish to do this?

share|improve this question
    
I am not aware of anything. I suspect you would have to some hacking/patching of your favorite screen saver application. – Zoredache Oct 31 '10 at 2:47
up vote 7 down vote accepted

It is possible, albeit a bit tricky. GnomeScreensaver documentation states:

Is there a way to perform actions when the screensaver activates or deactivates? Or when the session becomes idle?

One way is to watch for the D-Bus signals from gnome-screensaver. Here's an example of how to perform actions when the session becomes idle or is no longer idle using the Perl language:

my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver',member='SessionIdleChanged'\"";

open (IN, "$cmd |");

while (<IN>) {
    if (m/^\s+boolean true/) {
        print "*** Session is idle ***\n";
    } elsif (m/^\s+boolean false/) {
        print "*** Session is no longer idle ***\n";
    }
}

Although when I examined using dbus-monitor and proceeded to lock/unlock the screen the signal emitted was

path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=ActiveChanged
boolean true

when screen was locked and

path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=ActiveChanged
boolean false

When unlocked.

So altering the above script,

my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged'\"";

open (IN, "$cmd |");

while (<IN>) {
    if (m/^\s+boolean false/) {
        exec('/path/to/your/script');
    } 
}

should do it.

share|improve this answer

Building on the bash shell script Michael wrote:

#!/usr/bin/env bash
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged'" | while read line ; do 
    if [ x"$(echo "$line" | grep 'boolean true')" != x ] ; then 
        # runs once when screensaver comes on...
    fi
    if [ x"$(echo "$line" | grep 'boolean false')" != x ] ; then 
        # runs once when screensaver goes off...
    fi
done
share|improve this answer
    
Thank you! Very useful start point – ruX Nov 8 '13 at 14:37

Doing it in bash to share the same external script:

#!/usr/bin/env bash
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged'" | while read line ; do 
        if [ x"$(echo "$line" | grep 'boolean false')" != x ] ; then 
               # do whatever you want here
        fi
done
share|improve this answer

You must log in to answer this question.

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