scripts / rtcwake /
Newer Older
100 lines | 3.79kb
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
            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
}
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
49

            
50
case "$1/$2" in
51
    pre/suspend)
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
52
        set_RTCWAKE_TIME no
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
53
        ;;
54
    post/suspend)
fix post sleep
Sébastien MARQUE authored on 2019-01-06
55
        at -M now <<< "$0 post wait-for-trigger" > /dev/null 2>&1
56
        ;;
57
    post/wait-for-trigger)
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
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

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

            
93
            unset AWAKE_TIME
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