Last active
November 15, 2019 09:46
-
-
Save benjaoming/49015c0265f80249da080c41f79ceebc to your computer and use it in GitHub Desktop.
Automatically suspend using your own custom timing, unlike screen savers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Automatically suspend computer when the user has fallen asleep. But using your | |
own custom timing. | |
Add to your crontab with `crontab -e` | |
# m h dom mon dow command | |
0 2 * * * python3 /path/to/awake_or_suspend.py | |
This command either needs that your user can invoke `sudo` without a password or that it's launched with sude permissions. | |
Source: | |
https://gist.github.com/benjaoming/49015c0265f80249da080c41f79ceebc | |
""" | |
import gi | |
import os | |
import threading | |
import time | |
gi.require_version('Gtk', '3.0') | |
from datetime import datetime, timedelta | |
from gi.repository import Gtk | |
def suspend(seconds): | |
time.sleep(seconds) | |
os.system("systemctl suspend -i") | |
class DialogWindow(Gtk.Window): | |
def __init__(self): | |
Gtk.Window.__init__(self, title="Dialog Example") | |
self.hide() | |
dialog = Gtk.MessageDialog( | |
self, | |
0, | |
Gtk.MessageType.WARNING, | |
Gtk.ButtonsType.CANCEL, | |
"Are you awake buddy?" | |
) | |
seconds = 60 * 10 # 10 minutes | |
when = datetime.now() + timedelta(seconds=seconds) | |
dialog.format_secondary_text( | |
"I'd also like some sleep! Press 'Cancel' before " | |
"{:%H:%M:%S} or I'm gonna suspend myself.".format(when) | |
) | |
dialog.set_default_size(100, 100) | |
t = threading.Thread(target=suspend, args=(seconds,), daemon=True) | |
t.start() | |
response = dialog.run() | |
# if response == Gtk.ResponseType.CANCEL: | |
# print("The OK button was clicked") | |
dialog.destroy() | |
raise SystemExit | |
win = DialogWindow() | |
win.connect("delete-event", Gtk.main_quit) | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment