Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 44 lines (33 sloc) 1.01 KB
#!/usr/bin/env python3
import subprocess
import time
import re
import os
threshold = 5 # percent
sleep_time = 10 # seconds
warning_shown = False
# Kill old instances
mypid = os.getpid()
cmd = "(ps ax | grep battery-notify | perl -lne '/^\s*(\d+)/; print $1' | grep -v {} | xargs kill 2>/dev/null) || true"
subprocess.call(cmd.format(mypid), shell = True)
while True:
capacity = 0
nbatteries = 0
batinfo = ""
while True:
cmd = "acpiconf -i {} 2>&1".format(nbatteries)
try:
batinfo = subprocess.check_output(cmd, shell = True)
except Exception as e:
break # no more batteries
batinfo = batinfo.decode('utf-8')
m = re.search("""Remaining capacity:\s+(\d+)%""", batinfo)
capacity += int(m.group(1))
nbatteries += 1
capacity = capacity / nbatteries
if capacity > threshold:
warning_shown = False
elif capacity <= threshold and not warning_shown:
subprocess.call("notify-send -u critical 'LOW BATTARY!!!'", shell = True)
warning_shown = True
time.sleep(sleep_time)