Python遅延シャットダウン
退屈中.夜はパソコンをつけて寝て、しばらくしてから電源を切りたいです.小さなプログラムを書きました.
# -*- coding:utf8 -*-
# This program is created for auto power off my windows :)
# Author: fengxing
# Data: 2011-8-30
import sys
import os
import time
from datetime import datetime
def Usage():
print 'Usage:', os.path.basename(__file__), '[-t HH:MM] | delata{h|m}'
# shutdown computer after time_diff seconds
def shutdown(time_diff):
assert time_diff > 0
time.sleep(time_diff)
os.system('shutdown -s -f -t 60')
def main():
timeto = -1;
if len(sys.argv) == 2:
dat = sys.argv[1]
if dat.endswith('h'):
timeto = int(dat[0:-1]) * 3600
elif sys.argv[1].endswith('m'):
timeto = int(dat[0:-1]) * 60
elif len(sys.argv) == 3 and sys.argv[1] == '-t':
tm = sys.argv[2].split(':')
if len(tm) == 2:
now = datetime.today()
ends = datetime(now.year, now.month, now.day,
int(tm[0]), int(tm[1]))
timeto = (ends - now).seconds #don't care about day
if timeto < 0:
Usage()
exit(1)
print 'sleep', timeto
shutdown(timeto)
if __name__ == '__main__':
main()