Skip to content

Instantly share code, notes, and snippets.

@odiak
Last active July 31, 2020 07:03
Show Gist options
  • Save odiak/9c094596a3e7dd88848cd0874983c168 to your computer and use it in GitHub Desktop.
Save odiak/9c094596a3e7dd88848cd0874983c168 to your computer and use it in GitHub Desktop.
#!/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