... | ... |
@@ -2,8 +2,6 @@ |
2 | 2 |
# /lib/systemd/system-sleep/rtcwake |
3 | 3 |
# chmod a+rx |
4 | 4 |
|
5 |
-set -e |
|
6 |
- |
|
7 | 5 |
AWAKE_TIME="01:23:45" # hour:minute:second |
8 | 6 |
GAP="10 seconds" # precision of awake time in date(1) format |
9 | 7 |
WAIT_TRIGGER='true' # test to go back to sleep when action is finished |
... | ... |
@@ -11,32 +9,91 @@ WAIT_TRIGGER='true' # test to go back to sleep when action is finished |
11 | 9 |
WAIT_TRIGGER_DELAY=60 # sleep duration in seconds between two tests with WAIT_TRIGGER |
12 | 10 |
MAX_TRIGGER=60 # don't go back to sleep if WAIT_TRIGGER isn't true |
13 | 11 |
# after WAIT_TRIGGER_DELAY * MAX_TRIGGER seconds |
12 |
+LOG=/dev/null |
|
13 |
+ |
|
14 |
+# same format than AWAKE_TIME but as an array of times in increase order |
|
15 |
+#AWAKE_TIMES=("01:23:45" "02:46:00" "13:57:00" "20:00:00") |
|
16 |
+#WAIT_TRIGGER_DELAYS=(60 60 60 60) |
|
17 |
+#WAIT_TRIGGERS=("true" "true" "true" "true") |
|
18 |
+#MAX_TRIGGERS=(60 60 60 60) |
|
19 |
+#ON_ERRORS=() |
|
20 |
+ |
|
21 |
+unset RTCWAKE_TIME AWAKE_TIME |
|
14 | 22 |
|
15 | 23 |
test -r $0.conf && source $0.conf # all previous variables can be set here |
16 | 24 |
|
17 |
-NOW=$(date +%_H%M%S) |
|
18 |
-RTCWAKE_TIME=$([ $NOW -lt ${AWAKE_TIME//:} ] && date +%s -d $AWAKE_TIME || date +%s -d "tomorrow $AWAKE_TIME") |
|
25 |
+echo -e "\n$(date) $1/$2" >> $LOG |
|
26 |
+ |
|
27 |
+function set_RTCWAKE_TIME () { |
|
28 |
+ NOW=$(date +%_H%M%S) |
|
29 |
+ if test -z "$AWAKE_TIME"; then |
|
30 |
+ for ((i=0; i<${#AWAKE_TIMES[@]}; i++)); do |
|
31 |
+ echo "set_RTCWAKE_TIME: test $NOW -lt ${AWAKE_TIMES[i]//:}" >> $LOG |
|
32 |
+ if test $NOW -lt ${AWAKE_TIMES[i]//:}; then |
|
33 |
+ RTCWAKE_TIME=$(date +%s -d ${AWAKE_TIMES[i]}) |
|
34 |
+ echo "next wake up at ${AWAKE_TIMES[i]}" >> $LOG |
|
35 |
+ break |
|
36 |
+ elif test $i -eq $((${#AWAKE_TIMES[@]} - 1)); then |
|
37 |
+ RTCWAKE_TIME=$(date +%s -d "tomorrow ${AWAKE_TIMES[0]}") |
|
38 |
+ echo "next wake up tomorrow at ${AWAKE_TIMES[0]}" >> $LOG |
|
39 |
+ fi |
|
40 |
+ done |
|
41 |
+ else |
|
42 |
+ RTCWAKE_TIME=$([ $NOW -lt ${AWAKE_TIME//:} ] && date +%s -d $AWAKE_TIME || date +%s -d "tomorrow $AWAKE_TIME") |
|
43 |
+ fi |
|
44 |
+ suspend_duration=$(( $RTCWAKE_TIME - $(date +%s) )) |
|
45 |
+ date_awake=$(date -d "$suspend_duration seconds") |
|
46 |
+ echo "will be awaken in $suspend_duration seconds ($date_awake) (mode=$1)" >> $LOG |
|
47 |
+ rtcwake -m $1 -s $suspend_duration |
|
48 |
+} |
|
19 | 49 |
|
20 | 50 |
case "$1/$2" in |
21 | 51 |
pre/suspend) |
22 |
- rtcwake -m no -t $RTCWAKE_TIME |
|
52 |
+ set_RTCWAKE_TIME no |
|
23 | 53 |
;; |
24 | 54 |
post/suspend) |
25 | 55 |
at -M now <<< "$0 post wait-for-trigger" > /dev/null 2>&1 |
26 | 56 |
;; |
27 | 57 |
post/wait-for-trigger) |
28 |
- if test $NOW -ge ${AWAKE_TIME//:} -a $NOW -le $(date +%_H%M%S -d "$AWAKE_TIME $GAP"); then |
|
58 |
+ NOW=$(date +%_H%M%S) |
|
59 |
+ if test -z "$AWAKE_TIME"; then |
|
60 |
+ for ((i=0; i<${#AWAKE_TIMES[@]}; i++)); do |
|
61 |
+ if test $NOW -ge ${AWAKE_TIMES[i]//:} -a $NOW -le $(date +%_H%M%S -d "${AWAKE_TIMES[i]} $GAP"); then |
|
62 |
+ AWAKE_TIME=${AWAKE_TIMES[i]} |
|
63 |
+ WAIT_TRIGGER_DELAY=${WAIT_TRIGGER_DELAYS[i]:-60} |
|
64 |
+ MAX_TRIGGER=${MAX_TRIGGERS[i]:-60} |
|
65 |
+ WAIT_TRIGGER=${WAIT_TRIGGERS[i]:-true} |
|
66 |
+ test -n "${ON_ERRORS[i]}" && ON_ERROR=${ON_ERRORS[i]} |
|
67 |
+ test -n "${AWAKE_ACTIONS[i]}" && AWAKE_ACTION=${AWAKE_ACTIONS[i]} |
|
68 |
+ break |
|
69 |
+ fi |
|
70 |
+ done |
|
71 |
+ fi |
|
72 |
+ test -n "$AWAKE_ACTION" && at -M now <<< $AWAKE_ACTION > $LOG 2>&1 |
|
73 |
+ if test -n "$AWAKE_TIME" && test $NOW -ge ${AWAKE_TIME//:} -a $NOW -le $(date +%_H%M%S -d "$AWAKE_TIME $GAP"); then |
|
74 |
+ cat >> $LOG << EOL |
|
75 |
+NOW: $NOW |
|
76 |
+AWAKE_TIME: $AWAKE_TIME |
|
77 |
+WAIT_TRIGGER_DELAY: $WAIT_TRIGGER_DELAY |
|
78 |
+WAIT_TRIGGER: $WAIT_TRIGGER |
|
79 |
+MAX_TRIGGER: $MAX_TRIGGER |
|
80 |
+EOL |
|
81 |
+ |
|
29 | 82 |
COUNT=1 |
30 |
- while sleep $WAIT_TRIGGER_DELAY && eval $WAIT_TRIGGER; do |
|
31 |
- let COUNT++ && [ $COUNT -lt $MAX_TRIGGER ] || { |
|
83 |
+ while sleep $WAIT_TRIGGER_DELAY && eval "$WAIT_TRIGGER"; do |
|
84 |
+ let COUNT++ |
|
85 |
+ if test $COUNT -eq $MAX_TRIGGER; then |
|
32 | 86 |
if test -n "$ON_ERROR"; then |
33 | 87 |
eval "$ON_ERROR" |
34 | 88 |
fi |
35 | 89 |
break |
36 |
- } |
|
90 |
+ fi |
|
37 | 91 |
done |
38 |
- rtcwake -m mem -t $RTCWAKE_TIME |
|
92 |
+ |
|
93 |
+ unset AWAKE_TIME |
|
94 |
+ set_RTCWAKE_TIME mem |
|
39 | 95 |
else |
96 |
+ echo "rtcwake -m disable" >> $LOG |
|
40 | 97 |
rtcwake -m disable |
41 | 98 |
fi |
42 | 99 |
;; |