scripts / rtcwake /
Newer Older
100 lines | 3.757kb
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
                echo "next wake up at ${AWAKE_TIMES[i]}" >> $LOG
34
                break
35
            elif test $i -eq $((${#AWAKE_TIMES[@]} - 1)); then
36
                RTCWAKE_TIME=$(date +%s -d "tomorrow ${AWAKE_TIMES[0]}")
37
                echo "next wake up tomorrow at ${AWAKE_TIMES[0]}" >> $LOG
38
            fi
39
        done
40
    else
41
        RTCWAKE_TIME=$([ $NOW -lt ${AWAKE_TIME//:} ] && date +%s -d $AWAKE_TIME || date +%s -d "tomorrow $AWAKE_TIME")
42
    fi
43
    suspend_duration=$(( $RTCWAKE_TIME - $(date +%s) ))
44
    date_awake=$(date -d "$suspend_duration seconds")
45
    echo "will be awaken in $suspend_duration seconds ($date_awake) (mode=$1)" >> $LOG
46
    rtcwake -m $1 -s $suspend_duration
47
}
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
48

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

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

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