6

I want to know how I can detect whether the system is going to a standby mode in Linux using C. I found a message called WM_POWERBROADCAST in windows for that purpose, which sends this message before going to the sleep mode.

Is there any alternatives in C, for Linux?

I heard that DBus can be used for same purpose, could somebody explain it more?

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Harikrishnan
  • 3,664
  • 7
  • 48
  • 77

2 Answers2

6

Finally I found a solution.

We can use the pm utility for that.

If you put any shell script in /etc/pm/sleep.d folder it will be executed automatically just before the system going to sleep and after the system is resumed.

The content will be like


#!/bin/bash
case $1 in
suspend)
    #suspending to RAM
    /home/harikrishnan/Desktop/sleepd Sleeping
    ;;
resume)
    #resume from suspend
    sleep 3
    /home/harikrishnan/Desktop/sleepd Woken
    ;;
esac

here it will execute the /home/harikrishnan/Desktop/sleepd program with the arguments

Harikrishnan
  • 3,664
  • 7
  • 48
  • 77
2

AFAIK there's no such signal in Linux, but you can try

a) acpid daemon hooks, if its present, acpid configs are usually in /etc/acpi
b) DBus daemon hooks, again if its presend on a system
c) reading acpid sources to see how it gets the signals
d) writing your own kernel module

Community
  • 1
  • 1
zed_0xff
  • 32,417
  • 7
  • 53
  • 72