Score:2

Is there a way to "reset" idle time when unplugging laptop?

gy flag

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.

N0rbert avatar
zw flag
Does this answer your question? [Automatic suspend - "Computer will suspend very soon because of inactivity" - how can I disable this?](https://askubuntu.com/questions/1014965/automatic-suspend-computer-will-suspend-very-soon-because-of-inactivity-ho)
gy flag
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.
Jacob Vlijm avatar
by flag
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 avatar
by flag
Doktor J, what is your windowmanager (if you don't know, your Ubuntu version / DE)?
gy flag
Ubuntu 18.04.5 LTS running GNOME
WinEunuuchs2Unix avatar
in flag
Just run your finger over the track pad before you unplug it and close the lid.
gy flag
@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.
WinEunuuchs2Unix avatar
in flag
@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
Score:1
by flag

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()
gy flag
This sounds like a good answer to my issue... haven't had a chance to try it yet, but if it works tomorrow I'll be back to mark as accepted unless someone digs up some actual setting that addresses the issue :)
gy flag
also needed `xprintidle` installed ;)
Jacob Vlijm avatar
by flag
@DoktorJ Right! Added.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.