Skip to content

Instantly share code, notes, and snippets.

@kausality
Created March 1, 2015 18:23
Show Gist options
  • Select an option

  • Save kausality/42696da4e81813102305 to your computer and use it in GitHub Desktop.

Select an option

Save kausality/42696da4e81813102305 to your computer and use it in GitHub Desktop.
Linux low battery notifier
#!/usr/bin/python
import subprocess
import re
import time
# The location of the warning sound to play
PLAY='/home/kaustubh/zenbell.wav'
def loop(flag):
acpi=subprocess.check_output(['acpi','-b'])
status=re.search('Charging',acpi)
if status:
status='Charging'
else:
status='Discharging'
per=re.search('[0-9]+(?=%)',acpi) # The charge percentage of battery
if per:
per=int(per.group())
else:
per=100
if status=='Discharging' and per<=10:
if flag:
time.sleep(3*60) # If already notified then wait for 3+1 minutes
flag=0
else:
subprocess.call(['notify-send','Warning:','Battery running critically low'])
subprocess.call(['aplay',PLAY])
flag=1
else:
flag=0
return flag
def main():
flag=loop(0)
while True:
time.sleep(60)
flag=loop(flag)
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment