-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshutdownmonitor.py
More file actions
executable file
·60 lines (50 loc) · 1.33 KB
/
shutdownmonitor.py
File metadata and controls
executable file
·60 lines (50 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
"""
Copyright (c) 2018 Ian Shatwell
The above copyright notice and the LICENSE file shall be included with
all distributions of this software
"""
import sys
import signal
import time
import os
import psutil
import RPi.GPIO as GPIO
def signal_handler(signal, frame):
GPIO.cleanup()
sys.exit(0)
CPUWARNING = 80
PINSHUTDOWN = 17
PINHIGHCPU = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(PINSHUTDOWN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(PINHIGHCPU, GPIO.OUT)
# Register signal handler
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
cpuload = max(psutil.cpu_percent(interval=None, percpu=True))
countdown = 13
while True:
# Check cpu usage
cpuload = max(psutil.cpu_percent(interval=None, percpu=True))
if cpuload > CPUWARNING:
GPIO.output(PINHIGHCPU, 1)
else:
GPIO.output(PINHIGHCPU, 0)
# Look for shutdown signal
# Active low
if GPIO.input(PINSHUTDOWN):
countdown = 13
else:
countdown = countdown - 1
if countdown == 0:
print "BOOM!"
else:
print "Shutdown in {}".format(countdown)
if countdown == 0:
print "Shutdown command triggered"
os.system("sync")
time.sleep(3)
os.system("sync")
os.system("sudo shutdown -P now")
time.sleep(0.25)