2 contributor
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
AWAKE_TIME="01:23:45"
GAP="10 seconds"
WAIT_TRIGGER='true'
WAIT_TRIGGER_DELAY=60
MAX_TRIGGER=60
LOG=/dev/null
unset RTCWAKE_TIME AWAKE_TIME
test -r $0.conf && source $0.conf
echo -e "\n$(date) $1/$2" >> $LOG
case "$1/$2" in
pre/suspend)
NOW=$(date +%_H%M%S)
NOW_s=$(date +%s)
if test -z "$AWAKE_TIME"; then
for ((i=0; i<${#AWAKE_TIMES[@]}; i++)); do
if test $NOW -lt ${AWAKE_TIMES[i]//:}; then
RTCWAKE_TIME=$(date +%s -d ${AWAKE_TIMES[i]})
suspend_duration=$(( $RTCWAKE_TIME - $NOW_s ))
if test $suspend_duration -gt ${MIN_TIME_BEFORE_NEXT_AWAKE:-120}; then
break
fi
elif test $i -eq $((${#AWAKE_TIMES[@]} - 1)); then
RTCWAKE_TIME=$(date +%s -d "tomorrow ${AWAKE_TIMES[0]}")
fi
done
else
RTCWAKE_TIME=$([ $NOW -lt ${AWAKE_TIME//:} ] && date +%s -d $AWAKE_TIME || date +%s -d "tomorrow $AWAKE_TIME")
fi
suspend_duration=$(( $RTCWAKE_TIME - $NOW_s ))
date_awake=$(date -d "$suspend_duration seconds")
echo "$NOW: will be awaken in $suspend_duration seconds ($date_awake) (mode=$1)" >> $LOG
rtcwake -m no -s $suspend_duration
;;
post/suspend)
at -M now <<< "$0 post wait-for-trigger" > /dev/null 2>&1
;;
post/wait-for-trigger)
NOW=$(date +%_H%M%S)
if test -z "$AWAKE_TIME"; then
for ((i=0; i<${#AWAKE_TIMES[@]}; i++)); do
if test $NOW -ge ${AWAKE_TIMES[i]//:} -a $NOW -le $(date +%_H%M%S -d "${AWAKE_TIMES[i]} $GAP"); then
AWAKE_TIME=${AWAKE_TIMES[i]}
WAIT_TRIGGER_DELAY=${WAIT_TRIGGER_DELAYS[i]:-60}
MAX_TRIGGER=${MAX_TRIGGERS[i]:-60}
WAIT_TRIGGER=${WAIT_TRIGGERS[i]:-true}
test -n "${ON_ERRORS[i]}" && ON_ERROR=${ON_ERRORS[i]}
test -n "${AWAKE_ACTIONS[i]}" && AWAKE_ACTION=${AWAKE_ACTIONS[i]}
break
fi
done
fi
test -n "$AWAKE_ACTION" && at -M now <<< $AWAKE_ACTION >> $LOG 2>&1
if test -n "$AWAKE_TIME" && test $NOW -ge ${AWAKE_TIME//:} -a $NOW -le $(date +%_H%M%S -d "$AWAKE_TIME $GAP"); then
cat >> $LOG << EOL
NOW: $NOW
AWAKE_TIME: $AWAKE_TIME
WAIT_TRIGGER_DELAY: $WAIT_TRIGGER_DELAY
WAIT_TRIGGER: $WAIT_TRIGGER
MAX_TRIGGER: $MAX_TRIGGER
EOL
COUNT=1
while sleep $WAIT_TRIGGER_DELAY && eval "$WAIT_TRIGGER"; do
let COUNT++
if test $COUNT -gt $MAX_TRIGGER; then
if test -n "$ON_ERROR"; then
eval "$ON_ERROR"
fi
break
elif test -n "$STAY_AWAKE" && test -e $STAY_AWAKE; then
rm -f $STAY_AWAKE
exit
fi
done
systemctl suspend
else
echo "sortie de veille manuelle" >> $LOG
pkill -fx "/bin/bash $0 post wait-for-trigger"
rtcwake -m disable
fi
;;
esac