Update settime.sh

This commit is contained in:
nagumii 2024-12-20 16:03:37 +02:00
parent eeed2eac86
commit eac25f357c

View file

@ -1,33 +1,40 @@
#!/bin/bash
# Function to log both to the screen and syslog
log_message() {
echo "USB config: $1" # Echo to the screen
logger "USB config: $1" # Log to the system log
echo "$(date +"%Y-%m-%d %H:%M:%S") $1" >> /tmp/femtofox-config.log # Log to file
}
# 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
log_message "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
log_message "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
log_message "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
log_message "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
log_message "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
log_message "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