How to find a program which is preventing sleeping?

I am noticing that recently my laptop does not go to sleep/lockout after its configured X minutes of inactivity like it did in the past. I suspect some program (like media player does) is preventing it from going to sleep. I do not have any media player running, so how can I find which program is preventing it from sleeping?

It does go to sleep if I close the lid. But I would also like for it to go to sleep after X minutes of inactivity.

Update: Upgraded to Ubuntu 18.04 with default Gnome and Wayland enabled.


Solution 1:

I think I figured it out.

I used:

dbus-send --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.GetInhibitors

to get the list of inhibitors, like:

array [
   object path "/org/gnome/SessionManager/Inhibitor71"
   object path "/org/gnome/SessionManager/Inhibitor72"
]

Then I did:

dbus-send --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager/Inhibitor71 org.gnome.SessionManager.Inhibitor.GetAppId

Which returned:

string "firefox"

It seems Firefox is preventing it. It seems that now when YouTube is playing it prevents sleeping.

Solution 2:

Mitar is definitely the right answer, you can use dbus to retrieve the information.

The interesting aspect of it is that some tabs in Firefox will create the problem and others do not. As I tested now I can tell it's smart enough to add/remove the inhibition depending on the content of the page—i.e. on YouTube you have a video, that inhibits the screensaver.

I wrote a script in PHP in order to list the inhibitors as a simple list. I bet there are tools/libraries in python to do that (because many of the OS code uses that language) but unfortunately I don't know python enough to do that.

Copy the following in a file such as ~/bin/inhibitors and then make it executable with chmod 755 ~/bin/inhibitors. Now in your console you can type: inhibitors and you get a list of tools preventing your OS from going to sleep.

#!/usr/bin/php
<?php
$list = `dbus-send --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.GetInhibitors`;

$l = explode("\n", $list);

$found = false;
foreach($l as $a)
{
    $a = trim($a);
    if($found)
    {
        if($a == "]")
        {
            break;
        }
        if(substr($a, 0, 13) == 'object path "')
        {
            $inhibitor = substr($a, 13, strlen($a) - 14);

            $info = `dbus-send --print-reply --dest=org.gnome.SessionManager $inhibitor org.gnome.SessionManager.Inhibitor.GetAppId`;

            $names = explode("\n", $info);
            $n = trim($names[1]);
            if(substr($n, 0, 8) == 'string "')
            {
                $name = substr($n, 8, strlen($n) - 9);

                echo $inhibitor, " ", $name, "\n";
            }
        }
    }
    elseif($a == "array [")
    {
        $found = true;
    }
}

Here is an example of output:

$ inhibitors
/org/gnome/SessionManager/Inhibitor2414 firefox
/org/gnome/SessionManager/Inhibitor2415 org.gnome.Totem

In this case Firefox and Totem block my screensaver.