Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In a background Python script I need to detect, when the system just woke up from suspend. What is a good way that does not rely on a root script but rather on python modules such as DBus?

I'm new to dbus so I could really use some example code. From what I read it's related to

org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Resuming

Can anyone help me out with some code that connects the resuming signal to callback?

share|improve this question

2 Answers 2

The login1 interface provides the signal now. Here is the modified code:

#!/usr/bin/python
# slightly different code for handling suspend resume
# using login1 interface signals
#
import dbus      # for dbus communication (obviously)
import gobject   # main loop
from dbus.mainloop.glib import DBusGMainLoop # integration into the main loop

def handle_sleep_callback(sleeping):
  if sleeping:
    print "System going to hibernate or sleep"
  else:
    print "System just resumed from hibernate or suspend"

DBusGMainLoop(set_as_default=True) # integrate into main loob
bus = dbus.SystemBus()             # connect to dbus system wide
bus.add_signal_receiver(           # defince the signal to listen to
    handle_sleep_callback,            # name of callback function
    'PrepareForSleep',                 # signal name
    'org.freedesktop.login1.Manager',   # interface
    'org.freedesktop.login1'            # bus name
)

loop = gobject.MainLoop()          # define mainloop
loop.run()                         # run main loop
share|improve this answer
up vote 6 down vote accepted

Here is some example code that answers my question:

#!/usr/bin/python
# This is some example code on howto use dbus to detect when the system returns
#+from hibernation or suspend.

import dbus      # for dbus communication (obviously)
import gobject   # main loop
from dbus.mainloop.glib import DBusGMainLoop # integration into the main loop

def handle_resume_callback():
    print "System just resumed from hibernate or suspend"

DBusGMainLoop(set_as_default=True) # integrate into main loob
bus = dbus.SystemBus()             # connect to dbus system wide
bus.add_signal_receiver(           # defince the signal to listen to
    handle_resume_callback,            # name of callback function
    'Resuming',                        # singal name
    'org.freedesktop.UPower',          # interface
    'org.freedesktop.UPower'           # bus name
)

loop = gobject.MainLoop()          # define mainloop
loop.run()                         # run main loop

See the dbus-python tutorial.

share|improve this answer

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.