Created
August 28, 2020 13:50
-
-
Save grrowl/180a4b9af51ea217f76562342c62fa89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Hibernate When Network Idle" | |
date: 2020-04-14T20:57:40+10:00 | |
draft: true | |
tags: | |
- linux | |
--- | |
i have a pc at home which is often on, but not so often used. i'd like it to hibernate most of the time, unless i want to use it or it is doing something itself. it's possible, stringing together a few things: | |
### if the network's idle | |
Install [vnstat](https://wiki.archlinux.org/index.php/VnStat) | |
``` | |
pacman -Syu vnstat | |
``` | |
this allows us to query network usage split up by 5 minute intervals: | |
``` | |
~ › vnstat -i wlp2s0 -5 4 | |
wlp2s0 / 5 minute | |
time rx | tx | total | avg. rate | |
------------------------+-------------+-------------+--------------- | |
2020-04-15 | |
00:10 2.47 MiB | 835.14 KiB | 3.28 MiB | 91.85 kbit/s | |
00:15 913.94 KiB | 398.04 KiB | 1.28 MiB | 35.83 kbit/s | |
00:20 410.53 KiB | 301.39 KiB | 711.92 KiB | 19.44 kbit/s | |
00:25 657.27 KiB | 750.08 KiB | 1.37 MiB | 38.43 kbit/s | |
------------------------+-------------+-------------+--------------- | |
``` | |
using this we'll query the usage over the last 5 minutes using `jq` | |
``` | |
~ › vnstat -i wlp2s0 -5 1 --json 1 | jq ".interfaces[0].traffic.fiveminute[0]" | |
``` | |
```json | |
{ | |
"id": 169, | |
"date": { | |
"year": 2020, | |
"month": 4, | |
"day": 15 | |
}, | |
"time": { | |
"hour": 0, | |
"minute": 25 | |
}, | |
"rx": 673042, | |
"tx": 768082 | |
} | |
``` | |
### sleep for a while | |
by default this simply suspends, which is quick to start again, but isn't the lowest power consumption mode. | |
```sh | |
rtcwake --date "+30min" | |
``` | |
it gave me `rtcwake: write error` unless i explicitly specified `-m mem` | |
```sh | |
sudo rtcwake -m mem --date "+5min" | |
``` | |
```sh | |
or rtcwake -m no -l -t $(date +%s -d 'tomorrow 10:00')? | |
``` | |
https://jlk.fjfi.cvut.cz/arch/manpages/man/rtcwake.8.en | |
### cron job | |
we want to control when the napping will occur, and sleep for different times throughout the day. something like | |
* 3am ~ 6am: sleep time | |
* 6am ~ 10am: wake during morning | |
* 10am ~ 4pm: afternoon nap | |
* 4pm ~ 3am: wake during evening | |
``` | |
~~~~ | |
``` | |
### roll it into a script we can run periodically | |
```sh | |
#!/bin/zsh | |
# Usage: ./nap-if-idle.sh "+30min" | |
# arg 1 | |
# duration of nap | |
NAP_TIME="+30min" | |
# arg 2 | |
# how deep is the sleep | |
SLEEP_MODE="mem" | |
RX=$(vnstat -i wlp2s0 -5 1 --json 1 | jq ".interfaces[0].traffic.fiveminute[0].rx") | |
TX=$(vnstat -i wlp2s0 -5 1 --json 1 | jq ".interfaces[0].traffic.fiveminute[0].tx") | |
TOTAL_KB=(( $RX + $TX_)) | |
IDLE_KB=(( 1 * 1024 )) # 1 MiB | |
if [[ $TOTAL -gt $IDLE_KB ]]; then | |
echo 'System seems to be idle, having a nap' | systemd-cat | |
rtcwake --date $NAP_TIME | |
fi | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment