I have put together a script that sets a wakeup with the rtc in 10 minutes before a standby.
After waking up, it checks whether the phone was woken up by the rtc.
If this is the case, it checks for new messages, if there are none, a new standby is initiated.
I have not yet had any experience of how much it drains the battery etc. But basically it seems to work.
I would be interested in your opinion.
Wakeup 10 Minutes after Standby:
/etc/rtcwake_config.conf
# Default wake-up time (in seconds) during the day
DAYTIME_WAKEUP_SECONDS=600 # 10 minutes
# Night wake-up time (in seconds)
NIGHTTIME_WAKEUP_SECONDS=3600 # 60 minutes
# Defines the time range for the night (from hour 0 to hour 6)
NIGHT_START_HOUR=0
NIGHT_END_HOUR=6
/usr/local/bin/set-rtc-wakeup.sh
#!/bin/bash
# Source the configuration file
source /etc/rtcwake_config.conf
# Get the current hour (24-hour format)
HOUR=$(date +%H)
# Default wake-up time: 10 minutes (for daytime)
WAKE_AFTER_SECONDS=$DAYTIME_WAKEUP_SECONDS
# If it's nighttime (from 00:00 to 06:00), use the night wake-up time
if [ "$HOUR" -ge "$NIGHT_START_HOUR" ] && [ "$HOUR" -lt "$NIGHT_END_HOUR" ]; then
WAKE_AFTER_SECONDS=$NIGHTTIME_WAKEUP_SECONDS
fi
# Optional debug log (can be removed if not needed)
echo "[RTCWAKE] Hour: $HOUR, Wake in $WAKE_AFTER_SECONDS seconds" >> /var/log/rtcwake.log
# Set the RTC wake-up time
echo 0 > /sys/class/rtc/rtc0/wakealarm
rtcwake -m no -s $WAKE_AFTER_SECONDS
/etc/systemd/system/set-rtc-before-suspend.service
[Unit]
Description=Set RTC Wakeup before suspend
Before=sleep.target
ConditionPathExists=/usr/local/bin/set-rtc-wakeup.sh
[Service]
Type=oneshot
ExecStart=/usr/local/bin/set-rtc-wakeup.sh
[Install]
WantedBy=sleep.target
After standby and Wakeup from rtc, check For Messages, go to sleep if no new messages:
/etc/wakeup-check.conf
# Configuration for wakeup-check script
# Max time (in seconds) to wait for a working internet connection
MAX_WAIT=60
# Time (in seconds) to listen for incoming notifications before going to standby
NOTIFICATION_TIMEOUT=30
# Minutes until the next RTC wakeup if no notifications arrive
NEXT_RTC_WAKE_MIN=10
# Path to the log file
LOGFILE="/var/log/wakeup-check.log"
/usr/local/bin/wakeup-check.sh
#!/bin/bash
# ------------------ Load Config -------------------
CONFIG_FILE="/etc/wakeup-check.conf"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file $CONFIG_FILE not found!"
exit 1
fi
source "$CONFIG_FILE"
# ------------------ Logging Function ----------------
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOGFILE"
}
# ------------------ Check RTC Wakeup ----------------
is_rtc_wakeup() {
journalctl -k --since "2 minutes ago" | grep -qi "rtc"
}
# ------------------ Set RTC Wakeup ------------------
set_rtc_wakeup() {
WAKEUP_TS=$(( $(date +%s) + ($NEXT_RTC_WAKE_MIN * 60) ))
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo "$WAKEUP_TS" > /sys/class/rtc/rtc0/wakealarm
log "Next RTC wakeup set in $NEXT_RTC_WAKE_MIN minutes."
}
# ------------------ Wait for Internet ------------------
wait_for_internet() {
log "Checking internet connection via nmcli (max. $MAX_WAIT seconds)..."
for ((i = 0; i < MAX_WAIT; i++)); do
status=$(nmcli networking connectivity)
log "nmcli status: $status"
if [[ "$status" == "full" ]]; then
log "Full internet connection detected."
return 0
fi
sleep 1
done
log "nmcli failed – trying ping fallback..."
if ping -q -c 1 -W 2 8.8.8.8 >/dev/null; then
log "Ping successful – internet likely available."
return 0
else
log "Ping failed – no internet available."
return 1
fi
}
# ------------------ Main Logic ----------------------
log "---------- Wakeup script started ----------"
if is_rtc_wakeup; then
log "RTC wakeup detected."
# Start notification monitor in background
TMP_NOTIFY_FILE=$(mktemp)
log "Listening for notifications for $NOTIFICATION_TIMEOUT seconds..."
(timeout "$NOTIFICATION_TIMEOUT" dbus-monitor "interface='org.freedesktop.Notifications'" | grep -q "member=Notify" && echo "NOTIFIED" > "$TMP_NOTIFY_FILE") &
# Simultaneously wait for internet
wait_for_internet
INTERNET_READY=$?
wait # wait for dbus-monitor to finish
if grep -q "NOTIFIED" "$TMP_NOTIFY_FILE"; then
log "Notification received – staying awake."
else
log "No notifications received."
if [ "$INTERNET_READY" -eq 0 ]; then
set_rtc_wakeup
log "Going to suspend..."
systemctl suspend
else
log "No internet – staying awake for safety."
fi
fi
rm -f "$TMP_NOTIFY_FILE"
else
log "Wakeup not caused by RTC – likely user interaction. Staying awake."
fi
log "---------- Wakeup script finished ----------"
/etc/systemd/system/wakeup-check.service
[Unit]
Description=Wakeup Checker – detects RTC wakeup and suspends if idle
After=suspend.target network-online.target
Requires=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/wakeup-check.sh
[Install]
WantedBy=suspend.target