-
Notifications
You must be signed in to change notification settings - Fork 1
/
doublepress.sh
executable file
·50 lines (43 loc) · 937 Bytes
/
doublepress.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
#!/bin/bash
set -euo pipefail
name="$(basename $BASH_SOURCE)"
lock="/var/run/${name}.lock"
fifo="/var/run/${name}.fifo"
#lock="./${name}.lock"
#fifo="./${name}.fifo"
exec 200>"${lock}"
if ! flock -n 200; then
# slave instance, notify master
echo "1" > "${fifo}"
exit 0
fi
# master instance, owns lock
echo $$ 1>&200
# create fifo
rm -f "${fifo}" && mkfifo "${fifo}"
# cleanup fifo and lock file on exit
trap "rm \"${fifo}\" && rm \"${lock}\"" EXIT
# wait for slave instances signaling button press
counter=1
for i in {1..10}; do
if read -t 0.5 signal; then
echo -n '+' && ((counter+=1))
else
echo -n '.'
fi
done <>"${fifo}"
echo "($counter)"
case $counter in
2)
logger "doublepress: Suspend"
systemctl restart getty@tty?
systemctl suspend
;;
3)
logger "doublepress: Poweroff"
systemctl poweroff
;;
*)
logger "doublepress: Reject accidental Power-Button press"
;;
esac