windows 如何在启动时同步计算机的时钟?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/426071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:55:28  来源:igfitidea点击:

How can I sync the computer's clock upon startup?

windowsdatetimebatch-file

提问by ccoxtn

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.

我有几台托管在 GoGrid 上的服务器,每次我重新启动我的一台云服务器时,系统时钟都不正确。服务器不是域的成员,所以我只是将操作系统设置为与 Internet 时间服务器同步。这一天只发生一次,我没有看到在重新启动时自动发生这种情况的选项。所以我只剩下写一些代码来为我做这件事了。

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.

我使用“w32tm /resync”命令创建了一个批处理文件,并安排它在系统启动时运行,但它不起作用,因为运行批处理文件时网络连接不可用。如何在操作系统完全加载且网络连接可用后启动时间同步?我需要在计算机启动后尽快获得正确的时间,以便我的数据库中的时间戳正确。

采纳答案by ccoxtn

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.

在 Windows 2008 上,任务计划程序中有一个选项允许您在运行任务之前等待网络连接可用。在任务属性对话框中,创建一个新的“启动时”触发器,并确保选中“启用”框。添加一个操作来运行您的批处理文件。最后,添加条件以仅在网络连接可用时启动任务。然后我要求它在失败时最多重新启动命令 3 次。这似乎很好用。

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.

在旧版本的 Windows 上,使用以下批处理文件内容重试同步命令,直到成功,或者重试次数过多。

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

回答by Craig

In Windows 10, I went to services and enabled "Windows Time" service w32timeto be automatic, that solved it for me.

在 Windows 10 中,我转到服务并启用“Windows 时间”服务w32time为自动,这为我解决了这个问题。

My issue occured because I have a dual boot with Linux and for some reason Windows's clock is always off after I boot in Linux.

出现我的问题是因为我使用 Linux 进行双引导,并且出于某种原因,在 Linux 中引导后 Windows 的时钟总是关闭。

回答by Stever B

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

通常情况下,Windows 时间服务应该为您处理这个问题。

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

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

Do you have the service running?

你有服务运行吗?

回答by user2265240

As pointed out herethis may be related to having a dual boot system with windows 10 and linux.

正如此处所指出的,这可能与具有 Windows 10 和 linux 的双引导系统有关。

In my case the service was set to be run only manually and therefore was never triggered as one would suspect. Adjusting the service to be run automatically on startup within the services administration of windows solved the issue for me.

在我的情况下,该服务被设置为仅手动运行,因此从未像人们怀疑的那样被触发。在 Windows 的服务管理中将服务调整为在启动时自动运行为我解决了这个问题。

enter image description here

在此处输入图片说明

回答by equiman

Open a terminal (as Admin)and run this command:

打开终端(以管理员身份)并运行以下命令:

Use this on your home computer or not joined to domain
w32tm /resync

在您的家用计算机上使用它或未加入域
w32tm /resync

Use this if you are joined to domain
net time /domain

如果您已加入域,请使用此选项
net time /domain

If you get a The service has not been started. (0x80070426) error, then it means that you need to start the Windows Time service, wait a minute, and try again.

如果你得到一个 服务尚未启动。(0x80070426) 错误,则表示您需要启动 Windows Time 服务,稍等片刻,然后重试。

If you get a The computer did not resync because no time data was available error, then you should be able to try again until successful.

如果您收到 The computer did not resync because no time data is available 错误,那么您应该可以重试直到成功。

Windows 10: Synchronize Clock with an Internet Time Server in Windows 10

Windows 10:在 Windows 10 中将时钟与 Internet 时间服务器同步

回答by Ian Jacobs

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.

编辑:您可能希望在脚本中保持 30 秒左右,以允许刷新网络连接。完全没有正确阅读你的问题。

回答by scrappylaptop

Found this semi-randomly. New choice.exe syntax for Win 7+ to insert a 10-second pause

半随机地发现了这个。Win 7+的新choice.exe语法插入10秒暂停

choice /N /T 10 /D y>nul

回答by Stuart

I should have realized that I needed to run this batch file as an administrator for it to work, but once I figured that out, I only had to make a minor adjustment to the choice command for it to work like a charm.

我应该意识到我需要以管理员身份运行此批处理文件才能使其工作,但是一旦我想通了这一点,我只需要对选择命令进行微调即可让它像魅力一样工作。

choice /n /t:10 /D:y

Like the above user, I too have a dual boot between Ubuntu 18.04 and Windows 7, where booting to Ubuntu set's the System Clock to UTC and the OS adjusts for GMT, whereas it looks like Windozz sets the system clock to the time zone.

像上面的用户一样,我也有 Ubuntu 18.04 和 Windows 7 之间的双启动,其中启动到 Ubuntu 将系统时钟设置为 UTC,操作系统针对 GMT 进行调整,而看起来 Windozz 将系统时钟设置为时区。

Setting to the task scheduler was easy enough, my only specific issue is that I have my laptop connect to my FritzBox for time, so I guess my updates will only happen when I'm at home. (Its' easy enough to swap the NTP back to one of the presets, so I'm not concerned)

设置任务调度程序很容易,我唯一的具体问题是我的笔记本电脑连接到我的 FritzBox 时间,所以我想我的更新只会在我在家时发生。(很容易将 NTP 交换回预设之一,所以我不担心)