Prevent computer from sleeping in Linux

I need a way to prevent the computer from sleeping, turn it on/off at runtime.

I believe SetThreadExecutionState https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate?redirectedfrom=MSDN does that for windows(haven't tested yet, still looking for a cross-platform way)
Last edited on
There won't be any cross-platform way.
1. Not all platforms are capable of entering low-power states.
2. The specifics of such low-power states may be different.
3. Applications may not be able to signal that they're not idle to prevent the system from entering a low-power state.
4. How an application signals that it's not idle may be completely different from one system to another.
Thanks for the info. I should now change the topic title to "Prevent computer from sleeping in linux"

Do you know how can that be done in linux?
https://unix.stackexchange.com/questions/437735/prevent-system-from-going-to-sleep-suspend-how-xviewer-vlc-do-it

I don't know thing 1 about D-Bus, so I'm not going to be able to answer any follow-up questions you might have. You might want to move this thread to the UNIX forum.
Thanks anyway!
Linux is too vague, flavor and which environment would be useful but posting to the specific forum,Mint, Ubuntu,ect... would likely get you an answer faster.
Last edited on
After searching the net I found that the best way to do it is to use desktop.org using dbus

https://www.freedesktop.org/wiki/Software/systemd/inhibit/
https://people.gnome.org/~mccann/gnome-screensaver/docs/gnome-screensaver.html#gs-method-Inhibit

There are some C++ wrappers:
https://github.com/martinhaefner/simppl
https://github.com/makercrew/dbus-sample
https://dbus-cxx.github.io/
http://dbus-cplusplus.sourceforge.net/

And DBusBindings https://www.freedesktop.org/wiki/Software/DBusBindings/

I am on Qt and below is the code I am using which is a modified version of the code in this answer https://stackoverflow.com/a/54629429/11337328

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
void MainWindow::toggleSleepPevention()
{
#ifdef Q_OS_LINUX
    const int MAX_SERVICES = 2;

    QDBusConnection bus = QDBusConnection::sessionBus();
    if(bus.isConnected())
    {
        QString services[MAX_SERVICES] =
        {
            "org.freedesktop.ScreenSaver",
            "org.gnome.SessionManager"
        };
        QString paths[MAX_SERVICES] =
        {
            "/org/freedesktop/ScreenSaver",
            "/org/gnome/SessionManager"
        };


        static uint cookies[2];

        for(int i = 0; i < MAX_SERVICES ; i++)
        {
            QDBusInterface screenSaverInterface( services[i], paths[i],services[i], bus);

            if (!screenSaverInterface.isValid())
                continue;

            QDBusReply<uint> reply;

            if(preferences.preventSleep == true)
            {
                reply = screenSaverInterface.call("Inhibit", "nzuri-video Downloader", "REASON");
            }
            else
            {
                reply  = screenSaverInterface.call("UnInhibit", cookies[i]);
            }

            if (reply.isValid())
            {
                cookies[i] = reply.value();

                // qDebug()<<"succesful: " << reply;
            }
            else
            {
                // QDBusError error =reply.error();
                // qDebug()<<error.message()<<error.name();
            }
        }
    }

#elif defined  Q_OS_WIN

    EXECUTION_STATE result;

    if(preferences.preventSleep == true)
        result = SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
    else
        result = SetThreadExecutionState(ES_CONTINUOUS);

    if(result == NULL)
        qDebug() << "EXECUTION_STATE failed";

#endif
}
Last edited on
curious if its hardware or OS doing it?
seems like os would have a flag and not a kludgy workaround. Windows lets you turn it off entirely and linux should be even more configurable esp since a lot of unix machines are some sort of always on server setup.
it may be 'unfriendly' to flip the flag via your program, though.
Last edited on
it may be 'unfriendly' to flip the flag via your program, though.

Could you elaborate more on that?
depending on who its for (personal use? friends/ co-worker use? product / wide distribution?) and how they feel about it (wide distribution would be a no-go) you wouldn't want to change OS settings with your program as it affects everything not just your program. But if no one cares, that may be OK to do, eg personal use program or co-workers only etc.

And that is assuming it has one. Like I said, windows has one, and the linux guys tell me linux can do anything windows can but better, so I assume there is a GUI setting or OS level setting that prevents sleep... ? I dunno a lot about it at the user level; most of my experience is from wincscp / putty into a machine not running it locally. The last thing I ran locally was irix, which was .. 2005 ish? It was a fair bit different from linux.
Last edited on
These functions don't change settings, they're designed to signal to the system that they're doing uninterruptible work, to temporarily inhibit transitions to lower power states. For example, a video player preventing the screen from turning off and the system from sleeping while video is being played even if the user stops giving inputs.
Last edited on
@jonnin
You can read here https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate?redirectedfrom=MSDN

The doc says nothing about changing system settings.

Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.


The same is for Linux. Like @helios said, they just signal the system that they're doing uninterruptible work.
Topic archived. No new replies allowed.