13

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.

Mitar
  • 1,892
  • Please mention the distro, its version, and the desktop environment. – DK Bose Sep 03 '18 at 02:50
  • Your question will probably be closed as 'off-topic' because 17.10 is no longer supported. Use 16.04 or 18.04. – DK Bose Sep 03 '18 at 03:44
  • 1
    I think it is exactly the same thing? This has not really changed between versions. – Mitar Sep 03 '18 at 03:53
  • I would be interested in the answer to your question but that's the policy followed here. Using an unsupported version could lead to problems down the road. – DK Bose Sep 03 '18 at 04:02
  • 5
    I upgraded the system. The same thing is still there. – Mitar Sep 03 '18 at 07:14
  • Rather than Gnome & Wayland, try rebooting and selecting Gnome & Xorg. Does the suspend problem persist? – WinEunuuchs2Unix Sep 03 '18 at 20:26
  • Related: https://askubuntu.com/questions/1044901/how-do-i-tell-whats-preventing-sleep-on-ubuntu-my-box-wont-sleep-automaticall – Albert May 31 '22 at 06:58

5 Answers5

10

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.

Mitar
  • 1,892
  • Excellent, now we just need a little script that does all of that in one go. :-) – Alexis Wilke May 13 '20 at 19:27
  • @AlexisWilke, see my one-liner answer below – smac89 Jan 10 '21 at 10:21
  • 1
    I get Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files. Is this Gnome specific? I use KDE on Ubuntu 20.04. – Albert May 31 '22 at 06:56
2

Based on the answer from Alexis Wilke, I created a python solution:

https://pypi.org/project/list-session-inhibitors/

I have activated the Caffeine plugin, the Franz messaging app and I am running two Google meets in different accounts, one in Firefox and one in Chrome.

My script produces output like this:

tim@indigo:~$ inhibitors list
Listing inhibitors reported by dbus:
Inhibitor: /tmp/.mount_Franz-GuOnG8/franz Video Wake Lock
Inhibitor: /usr/bin/google-chrome-stable Video Wake Lock
Inhibitor: /usr/bin/google-chrome-stable WebRTC has active PeerConnections
Inhibitor: user Inhibit by Caffeine
Inhibitor: firefox audio-playing
Inhibitor: /usr/bin/google-chrome-stable Video Wake Lock
tim@indigo:~$ 

An alternative is systemd-inhibit --list but is not as helpful.:

systemd-inhibit --list
WHO                            UID  USER PID   COMM            WHAT                                                     WHY                                                       MODE 
ModemManager                   0    root 2222  ModemManager    sleep                                                    ModemManager needs to reset devices                       delay
NetworkManager                 0    root 1912  NetworkManager  sleep                                                    NetworkManager needs to turn off networks                 delay
UPower                         0    root 2654  upowerd         sleep                                                    Pause device polling                                      delay
Unattended Upgrades Shutdown   0    root 2449  unattended-upgr shutdown                                                 Stop ongoing upgrades or perform upgrades before shutdown delay
GNOME Shell                    1000 tim  5039  gnome-shell     sleep                                                    GNOME needs to lock the screen                            delay
Telepathy                      1000 tim  5156  mission-control shutdown:sleep                                           Disconnecting IM accounts before suspend/shutdown...      delay
franz                          1000 tim  69469 franz           sleep                                                    Application cleanup before suspend                        delay
gnome-tweak-tool-lid-inhibitor 1000 tim  5325  python3         handle-lid-switch                                        user preference                                           block
tim                            1000 tim  5255  gsd-media-keys  handle-power-key:handle-suspend-key:handle-hibernate-key GNOME handling keypresses                                 block
tim                            1000 tim  5020  gnome-session-b shutdown:sleep                                           user session inhibited                                    block
tim                            1000 tim  5255  gsd-media-keys  sleep                                                    GNOME handling keypresses                                 delay
tim                            1000 tim  5257  gsd-power       sleep                                                    GNOME needs to lock the screen                            delay
Tim Richardson
  • 2,294
  • 3
  • 25
  • 43
1

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.

Alexis Wilke
  • 2,707
1

This D-Bus command should work with any freedeskop-compliant power manager:

The following works only in XFCE

dbus-send --dest=org.freedesktop.PowerManagement --print-reply=literal /org/freedesktop/PowerManagement/Inhibit org.freedesktop.PowerManagement.Inhibit.GetInhibitors

The output was:

   array [
      Caffeine   ]
smac89
  • 854
  • I get Error org.freedesktop.DBus.Error.UnknownMethod: No such method 'GetInhibitors' in interface 'org.freedesktop.PowerManagement.Inhibit' at object path '/org/freedesktop/PowerManagement/Inhibit' (signature ''). – Albert May 31 '22 at 06:55
  • @Albert it seems you are right. The GetInhibitors method is only available in XFCE. See the updated answer – smac89 May 31 '22 at 20:14
  • Great. However, what if it returns array [ ] And refuses to sleep anyway? How to pick the wrongdoer process with top or ps aux? – Alexei Kouprianov Mar 01 '23 at 00:27
  • @AlexeiKouprianov in that case, you should be checking the system logs. It also depends on what method your DE is using for sleep. I don't remember what command XFCE uses when attempting to suspend, but what happens when you run the command (root not required) systemctl suspend? – smac89 Mar 01 '23 at 04:06
  • The screen turns black for a moment, then the login screen reappears. What to look for in the system logs though? Is there anywhere a simple step-by-step routine instruction on what to check and where? Apparently there is some process that prevents the system from sleeping but it would be strange to kill -9 them one by one to find the one that misbehaves. – Alexei Kouprianov Mar 06 '23 at 01:20
0

If you open a terminal window you can type "top" to view a continuously updating list of all running processes. One of these busy background processes is stopping your laptop from going to sleep. For a static list type "ps aux" instead. You can then see their process identification numbers. This will allow you to use "sudo kill pidnumber" on any suspect process, to see if stopping it will then allow your laptop to go to sleep (after its timeout period). Once you have identified the problematic process, you can then make an informed decision about whether that process is important to you.