3

Is there a (programmatic) way to prevent my Linux (Ubuntu) from going into sleep mode while my Python program is running? I seek a solution that avoids me manually changing the OS power settings each time I run my code.

Background: I'm running a long machine learning training from within PyCharm (using Ray RLlib). While I would have hoped for the intense active process to prevent the sleeping, my Ubuntu falls asleep after my standard power settings' suspension period is up if I don't interact during the training.

I am surprised to not find any solution when googling and looking at Stackoverflow; it would seem such a common need for Python (or maybe other) programmers, and I would have expected some python tools to exist to readily allow to prevent the sleeping from within my Python script itself.

I found SetThreadExecutionState for Windows (with C++), but nothing so far for Linux. A slightly less focused question Prevent sleep mode python (Wakelock on python) is also related to my issue, but it has not attracted any answer solving the case for the Linux OS.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
FlorianH
  • 600
  • 7
  • 18
  • 4
    Try `systemd-inhibit `! – Klaus D. Mar 30 '20 at 14:13
  • 1
    If you can do it "manually" you can do it automatically. Let your script set up power to prevent sleeping at its start and resume default values at its end. – tansy Mar 30 '20 at 14:24
  • 1
    Not directly. The problem is that there are tons of applications that work in background like for example a screen saver, so the inactivity timer only considers input interactions. I know no direct Python interface for that, you you will have to find a command that changes the timer behaviour and runs it through subprocess from your code. – Serge Ballesta Mar 30 '20 at 14:29
  • @tansy Thanks, correct. My script tends to crash, I tend to run sub-parts, etc.; so in worst case I should use that strategy but nicer would be a less 'system-settings-intrusive' way. – FlorianH Mar 30 '20 at 14:40
  • Thanks Serge Ballesta. That makes complete sense, I thought sth like that might be the case. Maybe the least intrusive albeit slightly involved way could be to create a small python facility emulating a mouse movement by + & - 1 px, say, if no input seems to have happened for the past 10min or so. I guess that is possible in a 'genuine' enough way such as for the OS to count it as user input. I might check out whether I can readily implement that and post it as answer if none else proposes sth ready-made (or tells me why I might be on the wrong track here). – FlorianH Mar 30 '20 at 14:44

2 Answers2

3

You can try Deluge-PreventSuspendPlus, version of Deluge-PreventSuspend-plugin. It is in python, uses DBus and Gnome Session Manager, but you can adapt it as well.

There are "inhibitor" classes in core.py where you will find how exactly it works.

tansy
  • 486
  • 3
  • 8
0

sudo systemctl mask sleep.target

sudo systemctl mask suspend.target

sudo systemctl mask hibernate.target

sudo systemctl mask hybrid-sleep.target

see the web page: https://linux-tips.us/how-to-disable-sleep-and-hibernation-on-ubuntu-server/

Soe
  • 1
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 21 '22 at 01:41
  • Seems like this solution will make your machine never sleep, rather than only during the running of your program. – Tim Kuipers Feb 16 '23 at 04:09