Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

3 Answers

up vote 5 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
add comment

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.