scripts / rtcwake /
Newer Older
102 lines | 3.865kb
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
1
#!/bin/bash
2
# /lib/systemd/system-sleep/rtcwake
3
# chmod a+rx
4

            
5
AWAKE_TIME="01:23:45" # hour:minute:second
6
GAP="10 seconds"      # precision of awake time in date(1) format
7
WAIT_TRIGGER='true'   # test to go back to sleep when action is finished 
8
                      # typically pgrep on user or for specific process
9
WAIT_TRIGGER_DELAY=60 # sleep duration in seconds between two tests with WAIT_TRIGGER
10
MAX_TRIGGER=60        # don't go back to sleep if WAIT_TRIGGER isn't true
11
                      # after WAIT_TRIGGER_DELAY * MAX_TRIGGER seconds
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
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
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
22

            
23
test -r $0.conf && source $0.conf # all previous variables can be set here
24

            
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
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
            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) ))
pas de suspension si prochai...
Sébastien Marque authored on 2019-11-03
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
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
49
}
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
50

            
51
case "$1/$2" in
52
    pre/suspend)
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
53
        set_RTCWAKE_TIME no
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
54
        ;;
55
    post/suspend)
fix post sleep
Sébastien MARQUE authored on 2019-01-06
56
        at -M now <<< "$0 post wait-for-trigger" > /dev/null 2>&1
57
        ;;
58
    post/wait-for-trigger)
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
59
        NOW=$(date +%_H%M%S)
60
        if test -z "$AWAKE_TIME"; then
61
            for ((i=0; i<${#AWAKE_TIMES[@]}; i++)); do
62
                if test $NOW -ge ${AWAKE_TIMES[i]//:} -a $NOW -le $(date +%_H%M%S -d "${AWAKE_TIMES[i]} $GAP"); then
63
                    AWAKE_TIME=${AWAKE_TIMES[i]}
64
                    WAIT_TRIGGER_DELAY=${WAIT_TRIGGER_DELAYS[i]:-60}
65
                    MAX_TRIGGER=${MAX_TRIGGERS[i]:-60}
66
                    WAIT_TRIGGER=${WAIT_TRIGGERS[i]:-true}
67
                    test -n "${ON_ERRORS[i]}" && ON_ERROR=${ON_ERRORS[i]}
68
                    test -n "${AWAKE_ACTIONS[i]}" && AWAKE_ACTION=${AWAKE_ACTIONS[i]}
69
                    break
70
                fi
71
            done
72
        fi
fix error
Sébastien MARQUE authored on 2019-10-31
73
        test -n "$AWAKE_ACTION" && at -M now <<< $AWAKE_ACTION >> $LOG 2>&1
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
74
        if test -n "$AWAKE_TIME" && test $NOW -ge ${AWAKE_TIME//:} -a $NOW -le $(date +%_H%M%S -d "$AWAKE_TIME $GAP"); then
75
            cat >> $LOG << EOL
76
NOW: $NOW
77
AWAKE_TIME: $AWAKE_TIME
78
WAIT_TRIGGER_DELAY: $WAIT_TRIGGER_DELAY
79
WAIT_TRIGGER: $WAIT_TRIGGER
80
MAX_TRIGGER: $MAX_TRIGGER
81
EOL
82

            
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
83
            COUNT=1
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
84
            while sleep $WAIT_TRIGGER_DELAY && eval "$WAIT_TRIGGER"; do
85
                let COUNT++
86
                if test $COUNT -eq $MAX_TRIGGER; then
erreur dans la conf
Sébastien MARQUE authored on 2019-02-25
87
                    if test -n "$ON_ERROR"; then
88
                        eval "$ON_ERROR"
envoi d'un mail en cas de no...
Sébastien MARQUE authored on 2019-02-17
89
                    fi
fix algo pour sortir
Sébastien MARQUE authored on 2019-03-30
90
                    break
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
91
                fi
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
92
            done
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
93

            
94
            unset AWAKE_TIME
fix error
Sébastien MARQUE authored on 2019-10-31
95
            test -r $0.conf && source $0.conf
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
96
            set_RTCWAKE_TIME mem
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
97
        else
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
98
            echo "rtcwake -m disable" >> $LOG
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
99
            rtcwake -m disable
100
        fi
101
        ;;
102
esac