You are not logged in.

#1 2015-11-01 15:24:36

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

NTP Time Sync Script

Hi everyone. I run BunsenLabs in a VM, and I have a nasty habit of leaving Virtualbox open when putting my computer into sleep/hibernate mode.. Usually this puts Virtualbox into suspend mode, and the clock stops on the VM's at the moment it's suspended.

So when I come back to my computer, my Host OS is fine, but the VM's I have running all of their clocks are way behind. Usually hours and hours at a time. So I wrote this script to check and correct the time skew. Anyone care to take a look, and see if there's any suggestions? I'm a total newbie at scripting, (as you can tell from some of my other posts tongue ) so 'constructive critisism' is appreciated!

#!/bin/bash


#Fix Time Skew
#Horizon_Brave

echo "Testing internet connection to ntp.pool.org..."
ping -c 4 ntp.pool.org > /dev/null
if [[ $? -ne 0 ]]
then 
echo "No internet connectivity!" 
exit 1
fi
echo "Test successful.. updating time..."

for time1 in $(sudo ntpdate 0.debian.pool.ntp.org | awk ' {print $10}' ); 
do 
echo "Time offset was: $time1";

offset=$(expr $time1 '>' 300 )

if [[ $offset -eq 1 ]]
then
echo "Time is insanely off...Let's sync again to get closer"
sudo ntpdate 0.debian.pool.ntp.org
fi
 
done;

if [[ $? -eq 0 ]]
then
echo "Time Sync'd! Thanks NTP!"
exit 0
else
echo "Time Sync failed."
exit 1
fi

"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#2 2015-11-01 21:09:01

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: NTP Time Sync Script

so you're running this inside the respective vms.

your if tests are a bit redundant, the first could be

if ping -c 4 ntp.pool.org
then ...

and the second could be

if [[ "$time1" -gt 300  ]]
then ...

apart from that it looks ok; supposing the ntp commands are working from the command line.

Offline

Board footer

Powered by FluxBB