Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to run a screen if the Gnome session is locked and unlocked. Is there a way that I can intercept this and perform certain actions when the desktop is locked or unlocked?

share|improve this question

1 Answer 1

up vote 20 down vote accepted

gnome-screensaver emits some signals on dbus when something happens.

Here the documentation (with some examples).

You could write a scripts that runs:

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"

and that does what you need anytime dbus-monitor prints a line about the screen locked/unlocked.


Here a bash command to do what you need:

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | ( while true; do read X; if echo $X | grep "boolean true" &> /dev/null; then SCREEN_LOCKED; elif echo $X | grep "boolean false" &> /dev/null; then SCREEN_UNLOCKED; fi done )

Just replace SCREEN_LOCKED and SCREEN_UNLOCKED with what you need.

share|improve this answer
    
Hi @peoro, That makes me think you can unlock or lock gnome screen/session from a scripted program, worth doing some ssh tricks about it ;-) –  Nikhil Mulley Jan 3 '12 at 5:13
    
@Nikhil: to do that you don't need to play around with dbus: gnome-screensaver-command is already there. Passing -a to gnome-screensaver-command you'll lock the screen, while you'll unlock it with -d. Anyway most gnome apps use dbus extensively, so you'll be able to do many amazing things with it. –  peoro Jan 3 '12 at 5:18
1  
@peoro Thanks great, very helpful! Can I also run this as some sort of daemon? When I enter this in the terminal now, it has to stay open to monitor the dbus for that case. I would like to execute this command at login and then it can be active during the entire session. –  Sander Jan 4 '12 at 16:13
    
I think things may have changed now in 2014? as the output doesnt change if the screen was only locked, it only shows something when it gets blanked and is very different from here :(, I created this question askubuntu.com/questions/505681/…, do you believe there is still some way to do that? thx! –  Aquarius Power Aug 2 at 2:18

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.