Last active
July 31, 2020 07:03
-
-
Save odiak/9c094596a3e7dd88848cd0874983c168 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
from subprocess import check_output, check_call | |
from datetime import datetime | |
import sys | |
from pathlib import Path | |
def main() -> None: | |
datetime_file = Path.home() / 'last-active-timestamp' | |
pause_file = Path.home() / 'pause-auto-shutdown' | |
outputs = check_output(('who',), encoding='utf8').splitlines() | |
outputs_without_tmux = tuple(line for line in outputs if 'tmux' not in line) | |
n = len(outputs_without_tmux) | |
now = datetime.now().timestamp() | |
threshold = 60 * 5 | |
if n > 0 or pause_file.exists(): | |
with open(datetime_file, 'w') as f: | |
f.write(str(now)) | |
return | |
try: | |
with open(datetime_file, 'r') as f: | |
d = float(f.read()) | |
except Exception: | |
return | |
if now - d > threshold: | |
datetime_file.unlink() | |
check_call(('sudo', '/sbin/shutdown', '-h', 'now')) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment