Skip to content

Instantly share code, notes, and snippets.

@TheRusskiy
Created November 9, 2013 11:39
Show Gist options
  • Save TheRusskiy/7384586 to your computer and use it in GitHub Desktop.
Save TheRusskiy/7384586 to your computer and use it in GitHub Desktop.
Execute shell command after a specified time period. E.g. shutdown computer.
import os
import datetime
import time
execute_after_minutes = 17
command = 'shutdown /h'
# 'shutdown /s' #shutdown
# 'shutdown /h' #hibernate
def now():
return datetime.datetime.now()
def is_time(the_time):
return now()>the_time
shift = datetime.timedelta(minutes = execute_after_minutes)
shutdown_time = datetime.datetime.now()+shift
while True:
if is_time(shutdown_time):
os.system(command)
break
diff = shutdown_time - now()
hours = diff.seconds / 3600
mins = (diff.seconds-hours*3600) / 60
secs = (diff.seconds-hours*3600-mins*60)
print( 'Left: '+str(hours)+'h '+str(mins)+'m '+str(secs)+'s' )
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment