Skip to content

Instantly share code, notes, and snippets.

@samrat
Created September 1, 2011 18:53
Show Gist options
  • Save samrat/1186941 to your computer and use it in GitHub Desktop.
Save samrat/1186941 to your computer and use it in GitHub Desktop.
Suspends laptop to save power during unexpected power outages(they're quite common in Nepal)
'''Checks the state of AC adapter(online or offline) and suspends the computer if offline'''
#while true ; do python2.7 loadsave.py ; sleep 5m; done
import os
comm = '''dbus-send --system --print-reply --dest="org.freedesktop.UPower" \
/org/freedesktop/UPower org.freedesktop.UPower.Suspend'''
def loadsave():
os.system("acpi -a > powerinfo.txt")
f = open('powerinfo.txt', 'r')
if f.read().strip() == "Adapter 0: off-line":
print "Putting computer to sleep"
os.system(comm) #suspend if ac power is not connected
if __name__ == '__main__':
loadsave()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment