views:

1058

answers:

3

I have several servers hosted with GoGrid, and every time I reboot one of my cloud servers, the system clock is incorrect. The server isn't a member of a domain, so I just have the OS set to sync with an Internet time server. This only happens once a day, and I don't see an option to make this happen automatically upon reboot. So I'm left with writing some code to do it for me.

I created a batch file with the "w32tm /resync" command, and scheduled it to run on system startup, but it doesn't work because the network connection isn't available when the batch file is run. How can I cause the time sync to start after the OS is fully loaded and the network connection is available? I need the time to be correct ASAP after the computer boots so timestamps in my database will be correct.

+1  A: 

scheduled task that runs on login maybe?

EDIT: you may want to put a 30 second or so hold in the script to allow for the network connections to refresh. Totally didn't read your question right.

Ian Jacobs
+3  A: 

Normally the Windows Time Service should take care of this for you.

http://technet.microsoft.com/en-us/library/cc773013.aspx

Do you have the service running?

Stever B
+1  A: 

On Windows 2008, there is an option in the Task Scheduler that will allow you to wait until a network connection is available before the task is run. In the task properties dialog, create a new "At Startup" trigger, and be sure to check the "Enabled" box. Add an action to run your batch file. Finally, add a Condition to start the task only if the network connection is available. I then asked it to restart the command up to 3 times if it failed. That seems to work great.

On older versions of Windows, use the following batch file contents to retry the sync command until it is successful, or until it has retried too many times.

REM *** Retry for up to 15 minutes (90 retries @ 10 seconds each)
set retryCount=0

:SyncStart
if %retryCount == 90 goto SyncEnd
set /A retryCount=retryCount + 1

REM *** Resync the system clock
w32tm /resync
if errorlevel 1 goto SyncDelay
if errorlevel 0 goto SyncEnd

:SyncDelay
REM *** If unsuccessful, delay 10 seconds, then retry
choice /n /t:y,10>nul
goto SyncStart

:SyncEnd
ccoxtn