批处理文件中的睡眠命令?
当我编写一个自动运行的批处理文件时,如何编写它以便在运行批处理文件时,它可以在命令之间暂停几秒钟?
上下文:
psexec \\server -u user -p pass cmd
[there needs to be a pause here for psexec to establish a connection]
dir /s >output.txt \\server\shared
*注意:我使用 psexec 在服务器端而不是本地运行 dir 命令的原因是因为在本地计算机上运行 dir 比远程运行快得多,而且时间至关重要。
当我手动执行此操作时,这显然很容易,我只需等待。但是,运行批处理文件会使其以接近即时的速度相邻运行所有命令,而不管最后一个命令的完成状态如何。我如何暂停?
When I'm writing a batch file to run automatically, how do I write it so that when the batch file is run, it can pause for a couple seconds in between commands?
Context:
psexec \\server -u user -p pass cmd
[there needs to be a pause here for psexec to establish a connection]
dir /s >output.txt \\server\shared
*Note: the reason I run the dir command server-side using psexec and not locally is because it's much faster to run dir on a local machine than remotely, and time is of the essence.
When I'm doing this by hand it's obviously easy, I just wait. But running a batch file makes it run all commands at near instant speeds next to each other, regardless of the completion status of the last command. How do I put in a pause?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Windows Vista / Windows 7 上,您可以使用超时命令:
在以前版本的 Windows 上,您可以使用 ping 命令(ping 命令在每次迭代之间有 1000 毫秒的延迟):
某些版本的 Windows(例如 Windows Server 2003)有 sleep.exe可执行文件:
注意:2003 年 Windows 资源工具包 包含 sleep.exe 命令。
如果您不知道 Windows 版本,只需使用 ping hack,因为它是可用的。
On Windows Vista / Windows 7 you can use the timeout command:
On previous versions of Windows, you can use the ping command (the ping command has 1000 ms of delay between each iteration):
Some versions of Windows (like Windows Server 2003) has the sleep.exe executable:
Note: Windows Resource kit for 2003 contains sleep.exe command.
If you don't know the Windows version, simply use the ping hack since it'll be available.
在较新版本的 Windows 中有
timeout
命令:数字后跟 T 是秒数。以上命令将等待 10 秒
There is
timeout
command in more recent version of Windows:number followed by T is number of seconds. Above command will wait for 10 seconds
2003 年的 Windows 资源工具包将安装在 Windows XP 上。它包含可以从命令批处理文件中使用的 SLEEP.EXE。
下载地址在这里 http://www.microsoft.com/download/en/详细信息.aspx?id=17657
Windows Resource kit for 2003 will install on Windows XP. It contains SLEEP.EXE which can be used from a command batch file.
download is here http://www.microsoft.com/download/en/details.aspx?id=17657
我认为这里的信息: http://malektips.com/xp_dos_0002.html 会比我更好地解释它但
仍然存在错误处理的情况(如果远程计算机没有启动怎么办?)。 cmd.exe 在大多数情况下对于执行任何远程活动都是毫无用处的,使用 powershell 可以实现更多功能。
编辑::
事实上,您可以使用 psexec 执行本地存储的程序(它会在服务器端本地复制并执行) - 使用它会是一个更可行的替代方案吗?
如果不知道您打算运行什么命令,就很难进一步进行。
编辑(2)::
如果它只是您正在运行的一个命令,只需将其存储在一个专用文件中,例如“remote_dir_listing.cmd”,然后使用 psexec :
这将强制将本地文件的副本复制到远程每次执行它时(如果您想扩展它)。通过这种方式,您完全不需要暂停 - 只有当 psexec 打开管道时它才会运行,一旦完成,它就会默默地关闭自己。
I think the information here: http://malektips.com/xp_dos_0002.html would explain it better than I.
There's still the case of error handling though (what if the remote machine isn't up?). cmd.exe is quite useless for doing any remote activities for the most part, using powershell would enable so much more.
EDIT::
In fact, you can execute a program stored locally with psexec (it gets copied across and executed locally server-side) - would using that be a more viable alternative?
Without knowing what commands you're intending to run it's hard to take it much further.
EDIT(2)::
If it's just the one command you're running, simply store it in a dedicated file, like 'remote_dir_listing.cmd', and then use psexec with:
This will force a copy of the local file to the remote side each time you execute it (in case you want to expand it). In this way, you bypass the need for a pause at all - only when psexec has got the pipes open will it run, and once it completes, it closes itself silently.