Hi Fredx.
Your script does what, besides telling us the timezone? I'm not sure that
I understand. Besides I have a deep hate of all snakes, without
discrimination, even computer ones!
Does your script set the time?
Of course I'm partial to using my own htpdate script!
BFN.
How to set time
Hi musher0
And then followed by ntpdate command, the correct time will be set
But there are more ways to set the timezone and time of course, I just like this one because it's all done automatically.
Fred
Running the python script will set the timezone automatically to yours.Your script does what, besides telling us the timezone?
And then followed by ntpdate command, the correct time will be set
Code: Select all
/usr/local/bin/tzupdate.py
sleep 1
ntpdate -s ntp.ubuntu.com
Fred
From what I tested it depends on the timezone correctly set. It doesn't "know" where you are.musher0 wrote:Hi fredx.
Thanks for the explanation. htpdate does it in one go.
BFN.
E.g. if I set timezone wrong, or to UTC, htpdate goes along with how it is set.
Actually same as how ntp works, it also needs the timezone to be set to your location.
Fred
I found that the code to get timezone and sync with ntpdate from here http://murga-linux.com/puppy/viewtopic. ... 17#1019617 using python script tzupdate.py doesn't always work (dependency python-requests required).
So here's a bash-only script that should do (but curl and ntpdate are required to be installed).
Also changed the way to check for network connection (using curl rather than ping, some network admins block ping requests, so using curl is more reliable).
Fred
So here's a bash-only script that should do (but curl and ntpdate are required to be installed).
Also changed the way to check for network connection (using curl rather than ping, some network admins block ping requests, so using curl is more reliable).
Code: Select all
#!/bin/bash
# automatically get your timezone and set the correct time from internet
# required is a working internet connection and to have ntpdate installed (to sync the time)
# to run at boot, put this script in /root/Startup
# use of curl to check network connection, ping doesn't always work well
# check network, wait until there is connection, will stay in loop until there's connection
case "$(curl -s --max-time 2 -I https://duckduckgo.com | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in
[23]) : ;;
*) echo "No network connection yet...";;
esac
while true
do
case "$(curl -s --max-time 2 -I https://duckduckgo.com | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in
[23]) echo "Connected !"; break;;
*) : ;;
esac
sleep 3
done
# get your timezone (from https://freegeoip.app)
TZONE=$(curl -s --request GET --url https://freegeoip.app/json/ --header 'accept: application/json' --header 'content-type: application/json' | tr ',' '\n' | grep "time_zone" | cut -d ":" -f2|sed s'/.$//;s/"//')
echo $ZONE
# set the timezone
if [ -f "/usr/share/zoneinfo/$TZONE" ]; then
ZONEFOUND=yes
ln -sf /usr/share/zoneinfo/$TZONE /etc/localtime
echo $TZONE > /etc/timezone
Xdialog --title " " --msgbox "$(gettext 'Timezone set to '$TZONE'.')" 0 0 &
else
Xdialog --title " " --msgbox "$(gettext 'Zone not exist! Cannot synchronize time.')" 0 0
fi
sleep 1
# synchronize time using ntpdate
[ $ZONEFOUND = yes ] && ntpdate -s ntp.ubuntu.com