首页 > 解决方案 > 在第一个持久程序使用批处理命令完成初始化后,如何执行第二个程序?

问题描述

我正在使用批处理文件来运行两个不同的可执行程序。我希望第二个程序在第一个程序完成初始化并在我的计算机上打开一个端口后运行。

我知道顺序命令排序的call/wait命令,尽管第一个程序是一个持久应用程序,它在我的计算机上打开一个端口;因此,它永远不会“完成”运行。关闭可执行文件的命令提示符会关闭端口。由于这种行为,如果我使用上述任一命令,则第二个调用的程序在第一个之前完成运行并导致问题。我意识到这种解释有些令人困惑,但如果需要澄清,我会提供更多背景信息。谢谢。

编辑:使用@shadoe2020 的代码并删除条件语句。

:: This file starts the mongod.exe and mongo.exe executables.
:: @ECHO OFF

start C:\Users\jacob\mongodb\bin\mongod.exe

:search
tasklist|find "mongod.exe"
TIMEOUT /T 3
GOTO found

:found
start C:\Users\jacob\mongodb\bin\mongo.exe
GOTO end

:end
PAUSE

标签: batch-file

解决方案


:: This file starts the mongod.exe and mongo.exe executables.
:: @ECHO OFF

start C:\Users\jacob\mongodb\bin\mongod.exe

:search
ver > nul
tasklist|find "mongod.exe"
IF %ERRORLEVEL% EQU 0 GOTO :found
TIMEOUT /T 5
GOTO search

:found
start C:\Users\jacob\mongodb\bin\mongo.exe
GOTO end

:end
PAUSE

在发现进程说 20 秒以允许程序 1 连接到您正在连接的任何东西之后,您甚至可以检查进程怎么样。


推荐阅读