Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 want to get notifications via dbus when the system resumes from suspended state. Following these existing questions:

  1. What DBus signal is sent on system suspend?
  2. How do I detect when my system wakes up from suspend via DBus or similar in a python app?

But none of the examples found in the links above fire when I suspend or resume. dbus-monitor does not see anything either:

 dbus-monitor --system "type='signal',interface='org.freedesktop.UPower'"

I also tried using this code to fire the signal manually (easier):

#taken from /usr/lib/systemd/system/upower.service
dbus-send --system --type=signal --dest=org.freedesktop.UPower \
    /org/freedesktop/UPower org.freedesktop.UPower.Resuming

Same result. I must be missing something really obvious. Fedora 20 x86_64. (dbus is installed, running and working fine AFAICT) Fedora 20 uses logind, but I cannot see any 'Resuming' signal there. Suspend and resume is difficult to test with VirtualBox, so I can't really compare with other OSes.

Interestingly, qdbus sees lots of services (org.gnome.SessionManager, etc..) , but nothing power related, but then again, it doesn't see login1 either..

qdbus | grep -i power | wc -l
0
share|improve this question
    
I don't think upower handles suspend and resume anymore; lists.freedesktop.org/archives/devkit-devel/2013-January/… – sciurus Feb 8 '14 at 14:39
    
@sciurus ok, so how am I supposed to get notification of "Resuming"? (will update the question to make it clearer this is what I want, upower or not) It is odd that the systemd service file is present and is meant to fire "Resuming" still, is it not? – totaam Feb 8 '14 at 15:09
    
I could be wrong, of course. :-) – sciurus Feb 8 '14 at 16:16
up vote 2 down vote accepted

According to this answer to the same question on the devkit mailing list used by upower, newer versions no longer emit that signal since this handled by systemd.

The replacement in systemd-land is logind, which has a signal called PrepareForSleep: "The PrepareForShutdown() resp. PrepareForSleep() signals are sent right before (with the argument True) and after (with the argument False) the system goes down for reboot/poweroff, resp. suspend/hibernate."

Here is a simple python script for watching suspend / resume events:

#!/usr/bin/env python

from datetime import datetime
import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop

def handle_sleep(*args):
    print "%s    PrepareForSleep%s" % (datetime.now().ctime(), args)

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

loop = gobject.MainLoop()
loop.run()
share|improve this answer

The above answer helped me a lot! But in case someone needs a Qt version...

#!/usr/bin/env python3

from datetime import datetime
import sys, dbus
from PyQt4.QtGui import QApplication
from dbus.mainloop.qt import DBusQtMainLoop

class DBusListener():

  def handle_sleep(self, suspended):
    print(datetime.now().ctime()," Suspended? ", str(suspended))

  def __init__(self):
    DBusQtMainLoop(set_as_default=True)    # integrate into Qt main loop
    bus = dbus.SystemBus()                 # connect to system wide dbus
    bus.add_signal_receiver(               # define the signal to listen to
      self.handle_sleep,                   # callback function
      'PrepareForSleep',                   # signal name
      'org.freedesktop.login1.Manager',    # interface
      'org.freedesktop.login1'             # bus name
    )

app = QApplication(sys.argv)
DBusListener()
app.exec_()
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.