1

Below is the code I got from this website to detect resume,

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

Can any one please help me, what is the signal name to detect power off condition.

1
  • You need to catch SIGTERM. That is a signal.
    – Rinzwind
    May 12, 2018 at 9:22

1 Answer 1

0

You can add this to catch SIGTERM, SIGINT and SIGKILL signals.

import signal

signal.signal(signal.SIGINT, sigterm_handler)
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGKILL, sigterm_handler)

You need SIGTERM for the power off. Mind though: pressing the buttons means the system will start a shutdown, so there is a limited amount of time before anything you want to do needs to be done.

5
  • Thanks for your answer, is there any alternative to handle Linux sleep(suspend) and resume without using dbus and gobject.
    – Subbu
    May 12, 2018 at 10:52
  • I doubt any other way would be easier. There will be a systemd service for each of these, You can add a hook to those.
    – Rinzwind
    May 12, 2018 at 11:45
  • Can you please suggest any link or example
    – Subbu
    May 12, 2018 at 11:50
  • unix.stackexchange.com/questions/124212/… has a couple of methods
    – Rinzwind
    May 12, 2018 at 11:52
  • According to this link: stackoverflow.com/questions/64282634/… , you cannot handle SIGKILL the same way as SIGTERM/SIGINT. How do you make it work?
    – user727680
    Jun 2, 2021 at 14:29

You must log in to answer this question.

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