Showing 1 changed files with 22 additions and 29 deletions
+22 -29
rtcwake
... ...
@@ -24,33 +24,29 @@ test -r $0.conf && source $0.conf # all previous variables can be set here
24 24
 
25 25
 echo -e "\n$(date) $1/$2" >> $LOG
26 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
-            if test $NOW -lt ${AWAKE_TIMES[i]//:}; then
32
-                RTCWAKE_TIME=$(date +%s -d ${AWAKE_TIMES[i]})
33
-                break
34
-            elif test $i -eq $((${#AWAKE_TIMES[@]} - 1)); then
35
-                RTCWAKE_TIME=$(date +%s -d "tomorrow ${AWAKE_TIMES[0]}")
36
-            fi
37
-        done
38
-    else
39
-        RTCWAKE_TIME=$([ $NOW -lt ${AWAKE_TIME//:} ] && date +%s -d $AWAKE_TIME || date +%s -d "tomorrow $AWAKE_TIME")
40
-    fi
41
-    suspend_duration=$(( $RTCWAKE_TIME - $(date +%s) ))
42
-    if test $suspend_duration -gt ${MIN_TIME_BEFORE_NEXT_AWAKE:-600}; then
43
-        date_awake=$(date -d "$suspend_duration seconds")
44
-        echo "$NOW: will be awaken in $suspend_duration seconds ($date_awake) (mode=$1)" >> $LOG
45
-        rtcwake -m $1 -s $suspend_duration
46
-    else
47
-        echo "$NOW: no need to suspend because next awake is scheduled in less than $((${MIN_TIME_BEFORE_NEXT_AWAKE:-600} / 60)) minutes"
48
-    fi
49
-}
50
-
51 27
 case "$1/$2" in
52 28
     pre/suspend)
53
-        set_RTCWAKE_TIME no
29
+        NOW=$(date +%_H%M%S)
30
+        NOW_s=$(date +%s)
31
+        if test -z "$AWAKE_TIME"; then
32
+            for ((i=0; i<${#AWAKE_TIMES[@]}; i++)); do
33
+                if test $NOW -lt ${AWAKE_TIMES[i]//:}; then
34
+                    RTCWAKE_TIME=$(date +%s -d ${AWAKE_TIMES[i]})
35
+                    suspend_duration=$(( $RTCWAKE_TIME - $NOW_s ))
36
+                    if test $suspend_duration -gt ${MIN_TIME_BEFORE_NEXT_AWAKE:-120}; then
37
+                        break
38
+                    fi
39
+                elif test $i -eq $((${#AWAKE_TIMES[@]} - 1)); then
40
+                    RTCWAKE_TIME=$(date +%s -d "tomorrow ${AWAKE_TIMES[0]}")
41
+                fi
42
+            done
43
+        else
44
+            RTCWAKE_TIME=$([ $NOW -lt ${AWAKE_TIME//:} ] && date +%s -d $AWAKE_TIME || date +%s -d "tomorrow $AWAKE_TIME")
45
+        fi
46
+        suspend_duration=$(( $RTCWAKE_TIME - $NOW_s ))
47
+        date_awake=$(date -d "$suspend_duration seconds")
48
+        echo "$NOW: will be awaken in $suspend_duration seconds ($date_awake) (mode=$1)" >> $LOG
49
+        rtcwake -m no -s $suspend_duration
54 50
         ;;
55 51
     post/suspend)
56 52
         at -M now <<< "$0 post wait-for-trigger" > /dev/null 2>&1
... ...
@@ -90,10 +86,7 @@ EOL
90 86
                     break
91 87
                 fi
92 88
             done
93
-
94
-            unset AWAKE_TIME
95
-            test -r $0.conf && source $0.conf
96
-            set_RTCWAKE_TIME mem
89
+            systemctl suspend
97 90
         else
98 91
             echo "rtcwake -m disable" >> $LOG
99 92
             rtcwake -m disable