moved time-setting code from rc.local to fire-once service

This commit is contained in:
nagumii 2024-12-17 20:09:09 +02:00
parent 12ac8bd1ec
commit 7fa22cb732
3 changed files with 47 additions and 29 deletions

View file

@ -34,32 +34,7 @@ echo 34 > /sys/class/gpio/export && echo out > /sys/class/gpio/gpio34/direction
# fi
# ) &
# if RTC recognized
if ls /dev/rtc* 1> /dev/null 2>&1; then
rtc="true"
if sudo hwclock -s 2>&1 | grep -a failed > /dev/null 2>&1; then
echo "RTC device found but failed to get time."
else
hwclock -s > /dev/null 2>&1 &
echo "RTC device found! $(date +"%Y-%m-%d %H:%M:%S %Z")."
fi
else
echo "RTC not found."
sudo date -s "1 JAN 2024 00:00:00" > /dev/null 2>&1 &
fi
# try get network time
date_str=$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g')
if [ -n "$date_str" ]; then
sudo date -s "$date_str" > /dev/null 2>&1
sudo echo "Got network time: $(date +"%Y-%m-%d %H:%M:%S %Z")."
if [ "$rtc" = "true" ]; then
echo "Setting RTC to network time."
sudo hwclock -w &
fi
else
echo "Failed to fetch network time, using $(date +"%Y-%m-%d %H:%M:%S %Z") instead."
fi
# necessary for internet over usb in otg mode (see https://web.archive.org/web/20241006173648/https://wiki.luckfox.com/Luckfox-Pico/Luckfox-Pico-Network-Sharing-1/)
#sleep 5 && route add default gw 172.32.0.100 && echo "nameserver 8.8.8.8" >> /etc/resolv.conf
@ -67,8 +42,6 @@ fi
# does not appear to lower power consumption, but needs more testing
#(sleep 5 && cpufreq-set -u 408000 && echo "Boot complete, setting CPU frequency to 408mhz.") &
(
sleep 5
# run USB automount and config tool
/usr/local/bin/usbconfig.sh
@ -85,8 +58,8 @@ fi
echo 0 > /sys/class/gpio/gpio34/value;
sleep 0.5;
done
)
)
echo "Boot complete."
) &
exit 0

View file

@ -0,0 +1,11 @@
[Unit]
Description=Run settime script at startup
Before=meshtasticd.service
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/settime.sh
[Install]
WantedBy=default.target

View file

@ -0,0 +1,34 @@
#!/bin/bash
# Check if the RTC (Real-Time Clock) has a valid time
if [[ $(hwclock -r) ]]; then
echo "Got time from RTC: $(hwclock -r)" # Output the time obtained from the RTC
time="$(hwclock -r)" # Store the RTC time in the 'time' variable
hwclock -s # Set the system time from the RTC time
fi
# Check if we can get the time from a network source (e.g., google.com)
if [[ ! $(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g') ]]; then
echo "Could not get time from network" # If no time is obtained, print an error message
else
# Get the time from the network and store it in the 'time' variable
time="$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g')"
echo "Got time from network: ${time}" # Output the time obtained from the network
date -s "${time}" # Set the system time from the network time
fi
# If no time is set yet, fall back to a default date
if [[ -z ${time} ]]; then
echo "Could not get time from any source. Setting to Jan 1 2024." # Inform the user of the fallback date
date -s "1 JAN 2024 00:00:00" # Set the system time to January 1st, 2024
fi
# Check if the RTC device exists
if [[ -z /dev/rtc* ]]; then
echo "No RTC found" # If no RTC device is found, print an error message
else
echo "RTC found, setting time from system clock" # If an RTC is found, set its time from the system clock
hwclock -w # Write the system time to the RTC
fi
exit 0 # Exit the script successfully