Skip to content

Instantly share code, notes, and snippets.

@ptone
Created November 19, 2009 17:42
Show Gist options
  • Save ptone/238925 to your computer and use it in GitHub Desktop.
Save ptone/238925 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Preston Holmes on 2009-11-18.
preston@ptone.com
Copyright (c) 2009
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
import os
import datetime
from subprocess import Popen, call, STDOUT, PIPE
dialog_script = """osascript -e 'tell app "system events" to activate' -e 'tell app "system events" to display dialog "Are you still here" buttons {"Yes"} giving up after 30'"""
def sh(cmd):
return Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE).communicate()[0]
def sleep():
sh('sudo pmset sleepnow')
def shutdown():
sh('sudo shutdown -h +10')
def main():
who = sh('who')
now = datetime.datetime.now()
hour = now.time().hour
weekday = now.weekday()
# don't apply this logic before 4 pm or if in maintenance
if hour < 16 or os.path.exists('/tmp/under_maintenance'):
return
if who:
if hour >= 20:
prompt_response = sh(dialog_script)
if 'true' in prompt_response:
sleep() # if no response from user after 8, sleep
else:
if weekday == 4 and hour >= 16: # if nobody logged in after 4pm on friday, shutdown
shutdown()
elif weekday > 4: # if nobody logged in on a weekend, shutdown
shutdown()
elif hour >= 16: # if nobody logged in after 4, sleep
sleep()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment