-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall_Shutdown_LED.sh
63 lines (52 loc) · 1.44 KB
/
Install_Shutdown_LED.sh
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
61
62
63
#!/bin/sh
enter_full_setting()
{
lua - "$1" "$2" <<EOF > "$2.bak"
local key=assert(arg[1])
local fn=assert(arg[2])
local file=assert(io.open(fn))
local made_change=False
for line in file:lines() do
if line:match("^#?%s*"..key) then
line=key
made_change=True
end
print(line)
end
if not made_change then
print(key)
end
EOF
mv "$2.bak" "$2"
}
echo 'enable UART to enable the power indicator LED'
enable_full_setting enable_uart=1 $CONFIG
echo 'writing python listener for power button'
sudo mkdir /opt/powerbutton
sudo cat > /opt/powerbutton/listen-for-shutdown.py << EOF
# listen to a power-button on Pin 5 (SCL)
# based on:
# https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi
import RPi.GPIO as GPIO
import subprocess
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)
print('Power Down')
subprocess.call(['shutdown', '-h', 'now'], shell=False)
EOF
echo 'writing systemd service for power button'
sudo cat > /etc/systemd/system/listen-for-shutdown.service << EOF
[Unit]
Description=Monitor the power-button and shutdown if pressed
[Service]
ExecStart=/usr/bin/python -u /opt/powerbutton/listen-for-shutdown.py
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
echo 'start and enable systemd service for power button'
sudo systemctl daemon-reload
sudo systemctl enable listen-for-shutdown.service
sudo systemctl start listen-for-shutdown.service