L
L
lolopolosko2013-02-15 20:19:38
linux
lolopolosko, 2013-02-15 20:19:38

Linux sleep signal?

Is there any way to find out the state of the system using C++?
One function needs to be executed before the system goes to sleep.
The function is designed to save the value and pull out the system time in order to correctly resume it later, since the program sometimes does not work as expected after waking up from sleep.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg, 2013-02-15
@elenbert

Everything can be done with shell scripts.
Create a file, for example, 10_myscript with the following content:

#!/bin/sh
case "$1" in
        hibernate|suspend)
                echo "Suspending myscript"
                ;;
        thaw|resume)
                echo "Resuming myscript"
                ;;
        *)
                echo "Unknown command"
                ;;      
esac
exit 0

The script itself is placed in /etc/pm/sleep.d
Actually, this script will be executed by the sleep/wake subsystem. This stub example handles 4 types of script arguments. In specific cases, you can do whatever you need - run another program with the necessary arguments, or send a message to another program via d-bus, etc.

I
ixSci, 2013-02-15
@ixSci

Once I asked a similar question, but I did not find any iron solution for all distributions. I did it through DBus and it suited me:
Subscribe

QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "Sleeping", this,  SIGNAL(SystemIsGoingToSuspend()));
QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "Resuming", this,  SIGNAL(SystemIsGoingToResume()));

Unsubscribe:
QDBusConnection::systemBus().disconnect("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower",  "Sleeping", this,  SIGNAL(SystemIsGoingToSuspend()));
QDBusConnection::systemBus().disconnect("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "Resuming", this,  SIGNAL(SystemIsGoingToResume()));

The code uses Qt, but I think the gist should be clear. When a certain event occurs, the SystemIsGoingToSuspend / SystemIsGoingToResume method will be called

E
egorinsk, 2013-02-17
@egorinsk

It would be much more logical, instead of installing crutches from shell scripts, to fix your program so that it works correctly over time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question