2

I have my laptop set to never suspend when plugged in, and to suspend after a period of time when on battery; this is the way I'd like to keep it.

I regularly switch between using my laptop and desktop, so sometimes my laptop sits plugged in but unused for a period of time. If I intend to use the laptop at my workbench that's no problem, but sometimes I want to unplug the laptop and take it with me to go work somewhere else. However, if it's been sitting idle long enough, I will either get a notification similar to this:

Automatic suspend notification

or if it's been long enough, the laptop will go right into suspend mode before I can react.

I'm literally picking up my laptop to use it, and would rather it not go into suspend immediately upon unplugging. Is there any setting I can change so that the inactivity timer restarts when unplugging my laptop? Failing that, is there any sort of workaround for this, such as some script that can be triggered by power management upon unplugging, that emulates some sort of activity to reset the timer?

EDIT 2021-09-17T18:21Z

I've attached a screenshot of my current power settings. This is how I'd like them to stay:

power settings

The problem is, if my laptop has been idle while plugged in for 29 minutes, then I unplug it, pick it up, walk into the living room, and sit down, it goes to sleep on me because it now thinks it's been idle for 30 minutes. I'd somehow like it to interpret connecting/disconnecting DC power as "activity", such that it would only suspend if I didn't touch the laptop for 30 minutes after unplugging it.

Jacob Vlijm
  • 83,767
Doktor J
  • 107
  • 9
  • 1
  • No, automatic suspend is already disabled when plugged in (which is how I want that); it's enabled when on battery power (also how I want it). The problem is that "inactive time" while plugged in gets counted towards the automatic suspend when I unplug it. – Doktor J Sep 17 '21 at 18:21
  • Close-voters: please read carefully. The linked supposed dupe is about another issue, this question is about disabled suspend for on-power, but deliberately enabled for battery, so suspend is jumping in when unplugging after time has passed for idle time for the latter. OP *does not want to unset suspend for battery*. – Jacob Vlijm Sep 18 '21 at 10:18
  • Doktor J, what is your windowmanager (if you don't know, your Ubuntu version / DE)? – Jacob Vlijm Sep 18 '21 at 13:20
  • Ubuntu 18.04.5 LTS running GNOME – Doktor J Sep 18 '21 at 20:58
  • Just run your finger over the track pad before you unplug it and close the lid. – WinEunuuchs2Unix Sep 23 '21 at 22:16
  • @WinEunuuchs2Unix that would require that I have a flawless memory, and remember to do that every time I unplug my laptop. The entire reason I posted this question is because I usually remember right around the time I'm pulling the plug and the screen goes black. – Doktor J Sep 30 '21 at 14:00
  • @DoktorJ In that case the easiest way is to run script every time laptop is unplugged. It would reset x idle: https://unix.stackexchange.com/questions/321917/executing-code-every-time-a-laptop-is-plugged-into-or-unplugged-from-power – WinEunuuchs2Unix Oct 01 '21 at 10:41

1 Answers1

1

Reset idle time when unplugging power, or reset idle time while you are on AC?

Even if you'd manage to create an action on unplugging (you can), the question is wether you would be in time to prevent suspending.

A more reliable trick is to prevent getting idle time pass the idle-time threshold at all, but only if you are on AC.

If you are on X, how can we do that?

Run a tiny daemon-like background script that - only when you are on AC and idle for more than (let's say) a minute - simulates a key press Control. That's a key that does nothing if you are not actually working on the laptop, only resets idle time.
Then if you unplug, the virtual keypress is skipped and the laptop will do whatever you set it to do after x time.
Below an example of such a script. In a comment, you mentioned you are using Ubuntu 18.04, Gnome, which means you are using Mutter (X), so you can use the tools in the script below.

How to use?

  1. Make sure xprintidle, xdotool and acpi are installed: sudo apt install xdotool acpi xprintidle
  2. Copy the script into an empty file.
  3. Test-run it from a terminal: python3 /path/to/script. If all works fine, add it to your startup commands.

The script

#!/usr/bin/env python3
import subprocess
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GLib, Gtk

def test(arg): # let's see what is the current idle time idletime = int(subprocess.check_output("xprintidle").decode("utf-8")) # and if we are plugged in onpower = "on-line" in subprocess.check_output( ["acpi", "-a"] ).decode("utf-8") # if idle for > 60 seconds AND on power, tap Ctrl to reset idle if all([idletime/1000 > 60, onpower]): subprocess.Popen( ["xdotool", "key", "Ctrl"] ) # keep it running please return True

GLib.timeout_add_seconds(15, test, None)

Gtk.main()

Jacob Vlijm
  • 83,767