Quantcast
Viewing all articles
Browse latest Browse all 5381

Beginners • Re: Raspberry Pi 5 RTC - Auto wake up and shutdown

I made a script update_time.sh, where I give the boot time and shutdown time and the scripts updates another script set_daily_alarm.sh to put the next boot time in the "wakealarm" and updates cron to shutdown the pi.

sudo /usr/local/bin/update_time.sh "08:00" "21:00"

update_time.sh

Code:

#!/bin/bash# Check if two arguments are providedif [ "$#" -ne 2 ]; then    echo "Usage: $0 <start_time> <end_time>"    exit 1fistart_time=$1  # e.g., "06:00"end_time=$2    # e.g., "23:00"# Directly set the next wake time/sbin/hwclock --hctosysecho 0 | sudo tee /sys/class/rtc/rtc0/wakealarm >/dev/nullecho $(date '+%s' -d "tomorrow ${start_time}") | sudo tee /sys/class/rtc/rtc0/wakealarm >/dev/null# Update set_daily_alarm.shset_daily_alarm_path="/usr/local/bin/set_daily_alarm.sh"sed -i "s/^echo \$(date '+%s' -d '.*')/echo \$(date '+%s' -d 'tomorrow $start_time')/" "$set_daily_alarm_path"# Update crontab(crontab -l | grep -v '/usr/local/bin/set_daily_alarm.sh'; echo "@reboot sleep 60 && sudo /bin/bash $set_daily_alarm_path") | crontab -(crontab -l | grep -v '/sbin/shutdown -h now'; echo "0 ${end_time:0:2} * * * sudo /sbin/shutdown -h now") | crontab -echo "Updated wake time to $start_time and shutdown time to $end_time."
set_daily_alarm.sh

Code:

#!/bin/bash/sbin/hwclock --hctosysecho 0 | sudo tee /sys/class/rtc/rtc0/wakealarm >/dev/nullecho $(date '+%s' -d 'tomorrow 08:00') | sudo tee /sys/class/rtc/rtc0/wakealarm >/dev/null
you should make the two scripts executable with sudo chmod +x

Once you start the script you can verify if the cron job has been updated: sudo crontab -e and check if the "wakealarm" has been set correctly with date -d @$(cat /sys/class/rtc/rtc0/wakealarm) and set_daily_alarm.sh has been updated.

I did not test this process for more than 1 day, so it could not "loop", but you get the basic idea. I am working on it to simplify the approach. Setting the time in wakealarm can be tricky if the time has already passed, you really need to put 'tomorrow xx:xx'.

Hope this helps

Statistics: Posted by MisterGin — Sat Feb 10, 2024 7:06 pm



Viewing all articles
Browse latest Browse all 5381

Trending Articles