Skip to content

Instantly share code, notes, and snippets.

@keiya
Created October 5, 2020 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keiya/4234e7df8fa20ea5af83d5116bbe60ff to your computer and use it in GitHub Desktop.
Save keiya/4234e7df8fa20ea5af83d5116bbe60ff to your computer and use it in GitHub Desktop.
auto shutdown script when inactive
import os
import pprint
from time import sleep
INACTIVE_TIME = 30
pp = pprint.PrettyPrinter(indent=4)
inactive_count = 0
while True:
session_cnt = int(os.popen('w -h | wc -l').read().rstrip())
#pp.pprint(session_cnt)
if session_cnt == 0:
inactive_count += 1
if inactive_count > INACTIVE_TIME:
os.system('/sbin/shutdown -h now')
else:
inactive_count = 0
sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment