9

I would like to be able to keep the computer awake during certain activities and longer running tasks, such as downloading larger files and watching full screen videos.

I like using Caffeine to keep the computer from sleeping by preventing the display from sleeping. This worked well for me on my mac, but this does not work in Ubuntu.

How can I do this?

3
  • Can be done, but how exactly should the two be connected? (I don't fully understand the caffeine- element) Sep 15, 2014 at 16:55
  • I use caffeine to keep the computer awake when I am downloading a large file or some other task where I will be away from the computer. Also full screen videos will prevent the display from sleeping when Caffeine is used.
    – Wes
    Sep 16, 2014 at 2:53
  • Would you care for a simple caffeine-like workaround? The difference is that on Ubuntu, blanking the screen / suspending is activated by idle time (using keyboard /mouse) and not by screen activity. Sep 16, 2014 at 10:21

4 Answers 4

7

I am not sure why Caffeine does not keep your computer awake. I suspect it is because idle time on Ubuntu is equivalent to the time there is no user activity detected via keyboard or mouse.

How to prevent the computer from falling asleep

If you are looking for a solution to keep your computer awake like Caffeine does (should do), the solution below should work; I tested it on 14.04. It exists of two small scripts that you should store together in one and the same folder. You can switch it of or on (toggle) with a key combination. Once you press it, it will show the current state in a message:

enter image description here or: enter image description here

It basically exists of a small background script that simulates a minor user action (keypress Ctrl) if idle time exceeds a defined amount of time, thus preventing the computer to go asleep or blank the screen. The keypress itself is meaningless and has no effect on playing video in full screen.

How to use

  • You will need to have xprintidle and xdotool installed:

    sudo apt-get install xprintidle xdotool
    

    (inotify-tools should be installed by default on Ubuntu, might not be the case on Xu- or Lubuntu)

  • The script below is to toggle on/off, copy it into an empty file, save it as insomnia.py (keep the name as it is!) and make it executable(!)

#!/usr/bin/env python3
import os
import subprocess

script_dir = os.path.dirname(os.path.abspath(__file__))
try:
    pid = subprocess.check_output(["pgrep", "-f", "caf.py"]).decode("utf-8").strip()
    subprocess.Popen(["kill", pid])
    subprocess.Popen(["notify-send", "Computer doesn't stay awake..."])
except:
    subprocess.Popen(["/bin/bash", "-c", script_dir+"/"+"caf.py"])
    subprocess.Popen(["notify-send", "Computer stays awake..."])
  • The (main) script below is to keep the computer awake, copy the script into an empty file, save it as caf.py (keep the name as it is!) and make it executable(!)
#!/usr/bin/env python3

import subprocess
import time

seconds = 120 # number of seconds to start preventing blank screen / suspend

while True:
    curr_idle = subprocess.check_output(["xprintidle"]).decode("utf-8").strip()
    if int(curr_idle) > seconds*1000:
        subprocess.call(["xdotool", "key", "Control_L"])
    time.sleep(10)

It is important that you keep both scripts in the same folder!

  • Now add a keyboard shortcut to toggle your Caffeine replacement on and off: "System Settings" > "Keyboard" > "Shortcuts" > "Custom Shortcuts"

    Add the command:

    /path/to/insomnia.py
    

    To a key combination of your choice

That's it.

2
  • @Wes is this what you were looking for? Sep 18, 2014 at 8:02
  • this is a terrible solution, poorly written, poorly explained and inefficient. Dec 22, 2023 at 14:48
1

xprintidle is not working here on Wayland and xdotool can be replaced with ydotool on Wayland.

There are more stories with org.gnome.ScreenSaver.SimulateUserActivity and gnome-session-inhibit but I have not settled with them.

If a script will set the value back, a solution can be:

gsettings set org.gnome.desktop.session idle-delay 3600

The number represents the seconds. Use 0 for Never.

0

You can also make use of the couple of script solutions here which in my opinion are more intelligent as they determine activity based on network activity or a folder size changing.

For example if you want to keep the computer awake while a backup or torrent client is working then use the second solution in the link by dhiya. It will check network traffic and keep it awake until traffic stops. Just remember to change 'wlan0' to 'eth0' in the code if you are using ethernet and not wireless lan.

UPDATE 17/09/2015: I modified dhiya's code from the link underneath to keep a system awake based on network traffic or user activity and to force graceful suspension if the desktop session ends up in the lock/login screen with no network traffic. The program logs everything to a log file. Config has also been provided to manage the logfile. I have named the program "Keep Awake".

LINK:Is there any way to make Ubuntu not to suspend while a download in progress?

0

Using Jacob Vlijm's really helpful script as a starting point, I've turned it into a full-fledged app with configuration options, battery awareness, etc.

You can grab it over at GitHub. No installation is necessary. Just:

  1. Download the .tar.gz and extract the files (or clone the master branch if you're a git user) to wherever you want the app to live. Having it on your $PATH is not necessary.
  2. Install the dependencies:

    sudo apt install xdotool xprintidle
    

    If you're using a laptop, you will also want acpi.

  3. From the directory you installed, run sleep_inhibit.
  4. If you want the app to automatically start every time you log in, open the icon's menu, select Preferences, and enable it in the preferences window.
  5. If you decide to uninstall it, quit Sleep Inhibit, then run cleanup.py, being sure to answer yes to all questions. Then delete the directory, and all traces will be gone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .