批处理文件中的睡眠命令?

发布于 2024-12-13 08:24:29 字数 407 浏览 1 评论 0原文

当我编写一个自动运行的批处理文件时,如何编写它以便在运行批处理文件时,它可以在命令之间暂停几秒钟?

上下文:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海的爱人是光 2024-12-20 08:24:29

在 Windows Vista / Windows 7 上,您可以使用超时命令:

timeout /T [delay in seconds] /NOBREAK > NUL

在以前版本的 Windows 上,您可以使用 ping 命令(ping 命令在每次迭代之间有 1000 毫秒的延迟):

ping -n [delay in seconds + 1] 127.0.0.1 > NUL

某些版本的 Windows(例如 Windows Server 2003)有 sleep.exe可执行文件:

sleep [delay in seconds]

注意:2003 年 Windows 资源工具包 包含 sleep.exe 命令。

如果您不知道 Windows 版本,只需使用 ping hack,因为它是可用的。

On Windows Vista / Windows 7 you can use the timeout command:

timeout /T [delay in seconds] /NOBREAK > NUL

On previous versions of Windows, you can use the ping command (the ping command has 1000 ms of delay between each iteration):

ping -n [delay in seconds + 1] 127.0.0.1 > NUL

Some versions of Windows (like Windows Server 2003) has the sleep.exe executable:

sleep [delay in seconds]

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.

云裳 2024-12-20 08:24:29

在较新版本的 Windows 中有 timeout 命令:

timeout /T 10

数字后跟 T 是秒数。以上命令将等待 10 秒

There is timeout command in more recent version of Windows:

timeout /T 10

number followed by T is number of seconds. Above command will wait for 10 seconds

慵挽 2024-12-20 08:24:29

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

ヅ她的身影、若隐若现 2024-12-20 08:24:29

我认为这里的信息: http://malektips.com/xp_dos_0002.html 会比我更好地解释它但

仍然存在错误处理的情况(如果远程计算机没有启动怎么办?)。 cmd.exe 在大多数情况下对于执行任何远程活动都是毫无用处的,使用 powershell 可以实现更多功能。

编辑::

事实上,您可以使用 psexec 执行本地存储的程序(它会在服务器端本地复制并执行) - 使用它会是一个更可行的替代方案吗?

如果不知道您打算运行什么命令,就很难进一步进行。

编辑(2)::

如果它只是您正在运行的一个命令,只需将其存储在一个专用文件中,例如“remote_dir_listing.cmd”,然后使用 psexec :

psexec \\server -u <user> -p <pass> -c -f remote_dir_listing.cmd

这将强制将本地文件的副本复制到远程每次执行它时(如果您想扩展它)。通过这种方式,您完全不需要暂停 - 只有当 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:

psexec \\server -u <user> -p <pass> -c -f remote_dir_listing.cmd

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文