#! /bin/sh set -eu THRESHOLD=15.00 is_charging() { [ "$(cat /sys/class/power_supply/axp20x-usb/online)" = "1" ] } is_inhibitor_active() { #dbus-send --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.GetInhibitors | tail -n+2 | grep object #INHIBITORS=$(sudo -u "$(cat /proc/$(pidof -s gnome-session-ctl)/environ | grep -z USER | sed 's,\x0,,;s,USER=,,')" DBUS_SESSION_BUS_ADDRESS=$(cat /proc/$(pidof -s gnome-session-ctl)/environ | grep -z DBUS_SESSION_BUS_ADDRESS | sed "s,\x0,,;s,DBUS_SESSION_BUS_ADDRESS=,,") gnome-session-inhibit --list) # printf "%s\n" "$INHIBITORS" INHIBITORS=$(systemd-inhibit --list --what=sleep --mode=block --no-legend \ | grep -e 'sleep') [ -n "$INHIBITORS" ] # if [ "$INHIBITORS" = "No inhibitors" ]; then # return 1 # fi # return 0 } is_screen_on() { return "$(cat /sys/class/backlight/backlight/bl_power)" } network_activity() { for n in $(seq 2) do for i in wwan0 wlan0 do A=$(ifstat -i $i 1 1 | tail -n1) RX=$(echo $A | awk '{print $1}') TX=$(echo $A | awk '{print $2}') if [ "$RX" = "n/a" ] || [ "$TX" = "n/a" ] then return 1 fi for v in $RX $TX do C=$(echo $v $THRESHOLD | awk '{if ($1 > $2) print "y"; else print "n"}') if [ "$C" = "y" ] then echo Ongoing network activity >&2 return 0 fi done done done return 1 } bluetooth_connected() { test "$(bluetoothctl devices Connected)" != "" return } hotspot_active() { nmcli c show --active | grep -e 'Hotspot' return } can_suspend() { ! ( is_inhibitor_active || is_screen_on || is_charging || bluetooth_connected|| hotspot_active || network_activity ) } cpufreq-set -g powersave while : do if can_suspend then (rtcwake -m no -s 600 && systemctl suspend) || : if can_suspend then sleep 30 fi else sleep 5 fi done