CALL 命令与带有 /WAIT 选项的 START

Posted

技术标签:

【中文标题】CALL 命令与带有 /WAIT 选项的 START【英文标题】:CALL command vs. START with /WAIT option 【发布时间】:2022-01-15 14:50:55 【问题描述】:

带有 WAIT 选项的 START 命令如何

START /wait notepad.exe 
START /wait  notepad.exe 

...与使用 CALL 命令有何不同?

CALL notepad.exe 
CALL notepad.exe 

是否存在这样一种情况,即一个人的行为可能与另一个人的行为不同,具体取决于正在执行的内容?

【问题讨论】:

看看technet.microsoft.com/en-us/library/bb491005.aspx是关于START,technet.microsoft.com/en-us/library/bb490873.aspx是关于CALL 【参考方案1】:

对于 exe 文件,我认为差异几乎不重要。 但是要启动一个exe,你甚至不需要CALL

当开始另一批时,这是一个很大的不同, 因为CALL 将在同一个窗口中启动它,并且被调用的批处理可以访问相同的变量上下文。 所以它也可以改变影响调用者的变量。

START 将为调用的批处理创建一个新的 cmd.exe,如果没有 /b,它将打开一个新窗口。 因为是新的上下文,所以不能共享变量。

区别

使用start /wait <prog> - 当<prog> 结束时,环境变量的更改会丢失 - 调用者一直等到<prog> 结束

使用call <prog> - 对于exe可以省略,因为等于刚启动<prog> - 对于exe-prog,调用者批处理等待或异步启动exe,但行为取决于exe本身。 - 对于 batch 文件,调用者批处理继续,当被调用的<batch-file> 完成时,不调用控件将不会返回调用者批处理

附录:

使用 CALL 可以更改参数(对于批处理和 exe 文件),但仅当它们包含插入符号或百分号时。

call myProg param1 param^^2 "param^3" %%path%%

将扩展为(从批处理文件中)

myProg param1 param2 param^^3 <content of path>

【讨论】:

当使用 START /WAIT 执行 file.bat 时,您需要指定 START /WAIT cmd /c "file.bat" 而不仅仅是 START /WAIT "file.bat",否则会创建 cmd 窗口for file.bat 将保持打开状态 您可以在以下位置找到 CALL 和 START 之间的比较:ss64.com/nt/start.html(今天更新了“开始/等待”和“开始与 CALL”部分) 我最喜欢的是start /wait /b cmd /c &lt;batchfile.bat&gt;,因为批处理文件在同一个命令窗口中一个接一个地运行 @linux64kb,但是对于批处理文件不是必须的,你只需要call batchfile.bat “setlocal”不是为你做的吗?【参考方案2】:

我认为它们的性能应该大致相同,但存在一些差异。 START 通常用于启动应用程序或启动给定文件类型的默认应用程序。这样,如果您 START http://mywebsite.com 它不会这样做 START iexplore.exe http://mywebsite.com

START myworddoc.docx 将启动 Microsoft Word 并打开 myworddoc.docx。CALL myworddoc.docx 做同样的事情......但是START 为窗口状态和类似性质的事物提供了更多选项。它还允许设置进程优先级和亲和性。

简而言之,鉴于 start 提供的附加选项,它应该是您的首选工具。

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
  [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
  [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
  [command/program] [parameters]

"title"     Title to display in window title bar.
path        Starting directory.
B           Start application without creating a new window. The
            application has ^C handling ignored. Unless the application
            enables ^C processing, ^Break is the only way to interrupt
            the application.
I           The new environment will be the original environment passed
            to the cmd.exe and not the current environment.
MIN         Start window minimized.
MAX         Start window maximized.
SEPARATE    Start 16-bit Windows program in separate memory space.
SHARED      Start 16-bit Windows program in shared memory space.
LOW         Start application in the IDLE priority class.
NORMAL      Start application in the NORMAL priority class.
HIGH        Start application in the HIGH priority class.
REALTIME    Start application in the REALTIME priority class.
ABOVENORMAL Start application in the ABOVENORMAL priority class.
BELOWNORMAL Start application in the BELOWNORMAL priority class.
NODE        Specifies the preferred Non-Uniform Memory Architecture (NUMA)
            node as a decimal integer.
AFFINITY    Specifies the processor affinity mask as a hexadecimal number.
            The process is restricted to running on these processors.

            The affinity mask is interpreted differently when /AFFINITY and
            /NODE are combined.  Specify the affinity mask as if the NUMA
            node's processor mask is right shifted to begin at bit zero.
            The process is restricted to running on those processors in
            common between the specified affinity mask and the NUMA node.
            If no processors are in common, the process is restricted to
            running on the specified NUMA node.
WAIT        Start application and wait for it to terminate.

【讨论】:

【参考方案3】:

callstart /wait 之间有一个有用的区别,例如在调用 regsvr32.exe /s 时,也被 Gary in 引用 在他对how-do-i-get-the-application-exit-code-from-a-windows-command-line的回答中@

call regsvr32.exe /s broken.dll
echo %errorlevel%

将始终返回 0 但

start /wait regsvr32.exe /s broken.dll
echo %errorlevel%

将从 regsvr32.exe 返回错误级别

【讨论】:

【参考方案4】:

致电

从另一个调用一个批处理程序而不停止父批处理程序。 call 命令接受标签作为调用的目标。在脚本或批处理文件之外使用时,调用在命令行中无效。 https://technet.microsoft.com/en-us/library/bb490873.aspx

开始

启动单独的命令提示符窗口以运行指定的程序或命令。不带参数使用时,启动会打开第二个命令提示符窗口。 https://technet.microsoft.com/en-us/library/bb491005.aspx

【讨论】:

【参考方案5】:

这是我在并行运行批处理文件时发现的(同一个 bat 文件的多个实例同时具有不同的输入参数):

假设您有一个执行长任务的 exe 文件,称为 LongRunningTask.exe

如果直接从bat文件中调用exe,只有第一次调用LongRunningTask会成功,其余的会报OS错误“File is already in use by the process”

如果你使用这个命令:

开始 /B /WAIT "" "LongRunningTask.exe" "参数"

您将能够运行 bat 和 exe 的多个实例,同时仍等待任务完成,然后 bat 继续执行剩余的命令。 /B 选项是为了避免创建另一个窗口,需要空引号才能使命令起作用,请参阅下面的参考。

请注意,如果您在开始时不使用 /WAIT,LongRunningTask 将与批处理文件中的其余命令同时执行,因此如果这些命令之一需要输出LongRunningTask

正在恢复:

这不能并行运行:

调用 LongRunningTask.exe

这将并行运行,只要命令的输出与 bat 文件的其余部分之间没有数据依赖关系,就可以了:

start /B "" "LongRunningTask.exe" "参数"

这将并行运行并等待任务完成,因此您可以使用输出:

启动 /B /WAIT "" "LongRunningTask.exe" "参数"

启动命令参考:How can I run a program from a batch file without leaving the console open after the program start?

【讨论】:

【参考方案6】:

这是一个旧线程,但我刚刚遇到这种情况并发现了一种巧妙的解决方法。我试图运行 setup.exe,但焦点正在返回到脚本的下一行,而无需等待 setup.exe 完成。我尝试了上述解决方案,但没有成功。

最后,通过更多管道传递命令起到了作用。

setup.exe 参数 |更多

【讨论】:

以上是关于CALL 命令与带有 /WAIT 选项的 START的主要内容,如果未能解决你的问题,请参考以下文章

Linux 命令(252)—— wait 命令(builtin)

Linux 命令(252)—— wait 命令(builtin)

Linux 命令(252)—— wait 命令(builtin)

使用带有列表的 shell=True 时忽略 subprocess.call() 参数 [重复]

在 discord.py wait_for 上带有 check() 的 TypeError

Javascript中的apply与call详解