scripts / rtcwake /
Newer Older
98 lines | 3.899kb
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

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

            
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
79
            COUNT=1
ajout multiples réveils/susp...
Sébastien Marque authored on 2019-10-27
80
            while sleep $WAIT_TRIGGER_DELAY && eval "$WAIT_TRIGGER"; do
81
                let COUNT++
fix délai avant mise en veil...
Sébastien MARQUE authored on 2019-12-01
82
                if test $COUNT -gt $MAX_TRIGGER; then
erreur dans la conf
Sébastien MARQUE authored on 2019-02-25
83
                    if test -n "$ON_ERROR"; then
84
                        eval "$ON_ERROR"
envoi d'un mail en cas de no...
Sébastien MARQUE authored on 2019-02-17
85
                    fi
fix algo pour sortir
Sébastien MARQUE authored on 2019-03-30
86
                    break
ajout mécanisme pour mainten...
Sébastien MARQUE authored on 2019-12-01
87
                elif test -n "$STAY_AWAKE" && test -e $STAY_AWAKE; then
88
                    rm -f $STAY_AWAKE
89
                    exit
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
systemctl suspend au lieu de...
Sébastien Marque authored on 2019-11-09
92
            systemctl suspend
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
93
        else
modification de message de l...
Sébastien MARQUE authored on 2019-12-01
94
            echo "sortie de veille manuelle" >> $LOG
ajout rtcwake pour réveil de...
Sébastien MARQUE authored on 2019-01-05
95
            rtcwake -m disable
96
        fi
97
        ;;
98
esac