#!/bin/bash # /lib/systemd/system-sleep/rtcwake # chmod a+rx set -e AWAKE_TIME="01:23:45" # hour:minute:second GAP="10 seconds" # precision of awake time in date(1) format WAIT_TRIGGER='true' # test to go back to sleep when action is finished # typically pgrep on user or for specific process WAIT_TRIGGER_DELAY=60 # sleep duration in seconds between two tests with WAIT_TRIGGER MAX_TRIGGER=60 # don't go back to sleep if WAIT_TRIGGER isn't true # after WAIT_TRIGGER_DELAY * MAX_TRIGGER seconds test -r $0.conf && source $0.conf # all previous variables can be set here NOW=$(date +%_H%M%S) RTCWAKE_TIME=$([ $NOW -lt ${AWAKE_TIME//:} ] && date +%s -d $AWAKE_TIME || date +%s -d "tomorrow $AWAKE_TIME") case "$1/$2" in pre/suspend) rtcwake -m no -t $RTCWAKE_TIME ;; post/suspend) at -M now <<< "$0 post wait-for-trigger" > /dev/null 2>&1 ;; post/wait-for-trigger) if test $NOW -ge ${AWAKE_TIME//:} -a $NOW -le $(date +%_H%M%S -d "$AWAKE_TIME $GAP"); then COUNT=1 while sleep $WAIT_TRIGGER_DELAY && eval $WAIT_TRIGGER; do let COUNT++ && [ $COUNT -lt $MAX_TRIGGER ] || { if test -n "$ON_ERROR"; then eval "$ON_ERROR" fi } done rtcwake -m mem -t $RTCWAKE_TIME else rtcwake -m disable fi ;; esac