Skip to content

Instantly share code, notes, and snippets.

@dmonn
Last active July 14, 2016 15:10
Show Gist options
  • Save dmonn/dafcb9f6c6813e302308ffac35afc79c to your computer and use it in GitHub Desktop.
Save dmonn/dafcb9f6c6813e302308ffac35afc79c to your computer and use it in GitHub Desktop.
Python Script to make a lightweight childlock using crons/windows scheduler
#!/usr/bin/env python
import os
import sys
import time
import datetime
# Script to kill specific programs outside of specified times
# E.G. for child locks
# Alert only work on windows
# Use crons or windows scheduler (call every minute) to make it work effectively
def get_settings():
return {
'STARTTIME': datetime.time(8,00),
'ENDTIME':datetime.time(18,00),
'NOTIFICATION_DELAY':'10', # Time in minutes before endtime, when notification should pop up
'NOTIFICATION_MESSAGE':'Steam is closed soon',
'PROGRAMS_TO_KILL':{'steam.exe'}, # Add programs to kill here, e.g. {'chrome.exe', 'steam.exe', 'firefox.exe'}
}
def in_time_range(start, end, time):
# Check if current time is in betweend start and end
if start <= datetime.time(time.hour, time.minute) <= end:
return True
else:
return False
def show_alert(title, text, style):
# Windows Messagebox
try:
import ctypes
ctypes.windll.user32.MessageBoxA(0, text, title, style)
except Exception:
pass
def main(arguments):
settings = get_settings()
current_time = datetime.datetime.now()
notification_time = (current_time - datetime.timedelta(minutes = int(settings['NOTIFICATION_DELAY'])))
if in_time_range(settings['STARTTIME'], settings['ENDTIME'], current_time):
# This is okay
if current_time == notification_time:
# Show notification x minutes before the endtime
try:
show_alert("Warning", settings['NOTIFICATION_MESSAGE'], 1)
except Exception:
pass
else:
# If time is not in range, kill the program
while not (in_time_range(settings['STARTTIME'], settings['ENDTIME'], current_time)):
# No way to start it during the 'no'-time
for program in settings['PROGRAMS_TO_KILL']:
os.system("TASKKILL /F /IM "+program)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment