Skip to content

Instantly share code, notes, and snippets.

@benjaoming
Last active November 15, 2019 09:46
Show Gist options
  • Select an option

  • Save benjaoming/49015c0265f80249da080c41f79ceebc to your computer and use it in GitHub Desktop.

Select an option

Save benjaoming/49015c0265f80249da080c41f79ceebc to your computer and use it in GitHub Desktop.

Revisions

  1. benjaoming revised this gist Nov 15, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion awake_or_suspend.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@
    # 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
    """
    @@ -25,7 +27,7 @@
    def suspend(seconds):

    time.sleep(seconds)
    os.system("systemctl suspend")
    os.system("systemctl suspend -i")


    class DialogWindow(Gtk.Window):
  2. benjaoming revised this gist Mar 13, 2017. No changes.
  3. benjaoming revised this gist Mar 13, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions awake_or_suspend.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@
    # m h dom mon dow command
    0 2 * * * python3 /path/to/awake_or_suspend.py
    Source:
    https://gist.github.com/benjaoming/49015c0265f80249da080c41f79ceebc
    """

    import gi
  4. benjaoming created this gist Mar 13, 2017.
    69 changes: 69 additions & 0 deletions awake_or_suspend.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    """
    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
    """

    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")


    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()