gpt4 book ai didi

windows - 在不使用 Find 命令的情况下检查服务是否正在运行

转载 作者:可可西里 更新时间:2023-11-01 09:39:26 24 4
gpt4 key购买 nike

在批处理文件中,我试图检查服务是否已启动,如果没有则等待。

现在检查服务是否正在运行,我正在这样做:

sc query "serviceName" | find /i "RUNNING"
if "%ERRORLEVEL%"=="0" (
echo serviceName is running.
) else (
echo serviceName is not running
)

问题是错误级别总是设置为 0。可能是因为这个已知的 Find bug .有没有其他方法可以检查服务是否已启动,如果没有则等待?

最佳答案

您可以使用 Findstr 代替 Find 命令:

sc query "Service name" | findstr /i "RUNNING" 1>nul 2>&1 && (
echo serviceName is running.
) || (
echo serviceName is not running
)

您也可以使用 wmic 命令来完成:

wmic service where name="Service name" get State | Findstr /I "Running" 1>NUL 2>&1 && (
echo serviceName is running.
) || (
echo serviceName is not running
)

将来要注意的另一件事是,在比较数值时,您不应将表达式用引号 "" 括起来,因此条件应如下所示:

If %ERRORLEVEL% EQU 0 () ELSE ()

关于windows - 在不使用 Find 命令的情况下检查服务是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19492191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com