You can run the (very low on juice) background script, which will suspend the computer, rounded on 10 seconds:
#!/usr/bin/env python3
import time
import subprocess
# --- set idle time below (seconds)
idle_set = 1200
# ---
def get_packets():
return int([l.strip().split()[1].replace("packets:", "") for l in \
subprocess.check_output("ifconfig").decode("utf-8").splitlines()\
if " RX " in l][0])
def get_idle():
return int(int(subprocess.check_output("xprintidle").decode("utf-8").strip())/1000)
data1 = get_packets()
t = 0
while True:
time.sleep(10)
data2 = get_packets()
if data2 - data1 > 3000:
t = 0
else:
t += 10
idletime = get_idle()
if all([idletime > idle_set, t > idle_set]):
subprocess.Popen(["systemctl", "suspend", "-i"])
data1 = data2
What it does
- Once per 10 seconds, it checks the current received amount of data, comparing it with 10 seconds ago (using
ifconfig
). If it exceeds a certain amount, the "counter" is set to zero, else a0 seconds is added to the "stream" -idle time.
- Also once per 10 seconds it looks at the "general" idle-time, using
xprintidle
If both exceed the set time (in the head of the script), the computer is put to sleep.
How to set up
The script needs xprintidle
sudo apt-get xprintidle
Copy the script into an empty file, save it as set_idle.py
- In the head section of the script, set the desired idle time
Test- rfun it by the command:
python3 /path/to/set_idle.py
If all works fine, add it to Startup Applications.
Note
This answer assumes traffic is via Ethernet connection. If not, the function get_packets()
possibly needs a small edit.