#!/bin/ash # Called to calculate the next wakeup of this machine. Should be called whenever # it might make sense to update this, e.g. right after boot, when entering/leaving # standby, and when shutting down. if [ "$1" = "test" ]; then shift TEST=1 SCHEDULE="$@" else SCHEDULE= TEST= . /opt/openslx/config fi # Figure out the next rtcwake SCHEDULE="$SCHEDULE $SLX_WAKEUP_SCHEDULE" if [ "$1" = "suspend" ]; then # If we're about to suspend, consider reboot and shutdown too - we # need to wake up to execute these SCHEDULE="$SCHEDULE $SLX_SHUTDOWN_SCHEDULE $SLX_REBOOT_SCHEDULE" fi # suspend == true SCHEDULE=$( echo ${SCHEDULE} ) # poor man's trim NEXT= if [ -n "$SCHEDULE" ]; then # Time-math in ash - yay TODAY=$(date +%Y-%m-%d) # Use tomorrow instead of adding 86400 seconds so it (hopefully) works when DST changes TOMORROW=$(date --date="+1 day" +%Y-%m-%d) NOW=$(date +%s) for t in $SCHEDULE; do HOUR=${t%%:*} MINUTE=${t##*:} [ -z "$HOUR" -o -z "$MINUTE" ] && continue [ "$HOUR" -lt 0 -o "$HOUR" -gt 23 ] && continue [ "$MINUTE" -lt 0 -o "$MINUTE" -gt 59 ] && continue # wake up 2 minutes early; for the shutdown/reboot times this is required # so the actual cronjob for the shutdown/reboot will run. # Otherwise, since the wakeup will take some seconds, the actual cronjob # will not trigger, since it lies a few seconds in the past. # For the scheduled wakeups this would't be needed, but two minutes # early shouldn't hurt anyone. TS=$(date --date "-2 minutes $TODAY $t" +%s) [ "$TS" -le "$NOW" ] && TS=$(date --date "-2 minutes $TOMORROW $t" +%s) if [ -z "$NEXT" ] || [ "$NEXT" -gt "$TS" ]; then NEXT="$TS" fi done fi if [ -n "$TEST" ]; then echo -n "Next wakeup: " date -d "@$NEXT" exit 0 fi # Enable WOL again, in case it got disabled /opt/openslx/scripts/cron-enable_wol if [ -n "$NEXT" ]; then # Set -l and hope it does the right thing on DST rtcwake -l -m no -t "$NEXT" else rtcwake -m disable fi exit 0