Skip to content

Instantly share code, notes, and snippets.

@endolith
Created July 29, 2009 03:11
Show Gist options
  • Select an option

  • Save endolith/157847 to your computer and use it in GitHub Desktop.

Select an option

Save endolith/157847 to your computer and use it in GitHub Desktop.
Reverse alarm clock

Use the Gnome screensaver to enforce bedtime or otherwise lock out the computer

Note that the actual time it warns the user is whenever you call the script, so you can use more granularity than an hour, can set it for different times on different days, etc.

Start the script with a crontab line like this:

05 23 * * 0-4 env DISPLAY=:0.0 /home/username/Applications/bedtime.py #JOB_ID_2

This starts the countdown at 11:05 PM Monday through Friday... I think. Crontab's syntax is unintuitive, indecipherable nonsense.

You need the DISPLAY bit or it won't work.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Use the Gnome screensaver to enforce bedtime or otherwise lock out the computer, and set an away message in Pidgin
# Note that the actual time it warns the user is whenever you call the script,
# so you can use more granularity than an hour, can set it for different times
# on different days, etc.
# You can enter your password and log back in if you want, but it will repeatedly lock you back out.
import time
import pynotify
from subprocess import Popen, PIPE
appname = 'Bedtime' # In case you want to use it for something else
bedtime = 12 + 9 # (hours) Time before which the script should refuse to run
morning = 5 # (hours) Time after which the script should quit and stop locking out the user
grace_period = 20 # (minutes) Time between warning and locking out
snooze_time = 10 # (minutes) Time between logging back in and being locked back out
away_message = 'enforced bedtime'
def current_hour():
"""Returns the current hour"""
return time.localtime().tm_hour
def notify(message):
"""Pops up a libnotify notification and prints"""
print message
pynotify.Notification(appname, message).show()
if morning < current_hour() < bedtime:
print 'Script started too early - quitting'
else:
pynotify.init(appname)
notify('You will be locked out in %(grace_period)s minutes' % locals())
time.sleep((grace_period - snooze_time) * 60)
while current_hour() >= bedtime or current_hour() < morning:
status = Popen(['gnome-screensaver-command', '--query'], stdout=PIPE).communicate()[0]
print status.split('\n')[0]
if 'inactive' in status:
notify('You will be locked out in %(snooze_time)s minutes' % locals())
time.sleep(snooze_time * 60)
Popen(['purple-remote', 'setstatus?status=away&message=' + away_message])
notify('Locking screen...')
time.sleep(4) # short pause so you see the notification that tells you why it's shutting down
Popen(['gnome-screensaver-command', '--lock'])
time.sleep(snooze_time * 60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment