How to set time

How to do things, solutions, recipes, tutorials
Message
Author
musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#21 Post by musher0 »

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.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#22 Post by fredx181 »

Hi musher0
Your script does what, besides telling us the timezone?
Running the python script will set the timezone automatically to yours.
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
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

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#23 Post by musher0 »

Hi fredx.

Thanks for the explanation. htpdate does it in one go.

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#24 Post by fredx181 »

musher0 wrote:Hi fredx.

Thanks for the explanation. htpdate does it in one go.

BFN.
From what I tested it depends on the timezone correctly set. It doesn't "know" where you are.
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

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#25 Post by fredx181 »

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).

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	
Fred

Post Reply