2008-12-09 123 views
39

我希望能够查询服务是否从Windows批处理文件运行。我知道我可以使用:如何从命令行测试服务是否正在运行

SC查询 “服务名称”

但是,这个转储出一些文字。我真正想要的是设置errorlevel环境变量,以便我可以对此采取措施。

你知道一个简单的方法可以做到吗?

UPDATE
感谢您的回答。我担心解析文本的解决方案可能无法在非英文操作系统上运行。有没有人知道解决这个问题的方法,或者我将不得不硬着头皮写一个控制台程序来实现这一点。

+0

我只是一个中国区域中国语言的Windows 7笔记本电脑测试中,“SC查询...”命令的输出仍然是英语。 – wangf 2015-08-31 07:15:13

+0

无论使用何种操作系统语言,都可以通过脚本轻松解析服务状态我已经使用过`WMIC服务WHERE“名称='SericeName'”GET Started/format:list`。它产生`State = Running` - 易于用正则表达式解析并且总是用英文。 – 2018-02-19 08:22:38

回答

63
sc query "ServiceName" | find "RUNNING" 
+1

找不到。 findstr的作品。 – 2015-07-30 08:07:11

+0

@imkheong,什么操作系统“发现”不工作?它在win7上... ... – 2016-08-03 07:31:44

+0

@ShaiAlon,它是powershell,它没有工作。它在通常的命令提示符(cmd.exe)上工作。 – 2016-08-04 09:00:33

1

尝试

sc query state= all 

的服务列表和它们是否正在运行与否。

8

如果您不介意将net命令与grep结合使用,则可以使用以下脚本。

@echo off 
net start | grep -x "Service" 
if %ERRORLEVEL% == 2 goto trouble 
if %ERRORLEVEL% == 1 goto stopped 
if %ERRORLEVEL% == 0 goto started 
echo unknown status 
goto end 
:trouble 
echo trouble 
goto end 
:started 
echo started 
goto end 
:stopped 
echo stopped 
goto end 
:end 
1

我发现这一点:

sc query "ServiceName" | findstr RUNNING 

似乎做大致正确的事情。但是,我担心这不足以在非英语操作系统上工作。

4

你可以使用WMIC与/locale选项

call wmic /locale:ms_409 service where (name="wsearch") get state /value | findstr State=Running 
if %ErrorLevel% EQU 0 (
    echo Running 
) else (
    echo Not running 
) 
2

这里的箱子,我要提出的是PowerShell的可能答案上了最新的XP/2003的机器和外思考一点点当然在Vista/2008和更新(而不是.bat/.cmd)。任何在他们的背景中都有一些Perl的人应该很快就会感到在家。


$serviceName = "ServiceName"; 
$serviceStatus = (get-service "$serviceName").Status; 

if ($serviceStatus -eq "Running") { 
    echo "Service is Running"; 
} 
else { 
    #Could be Stopped, Stopping, Paused, or even Starting... 
    echo "Service is $serviceStatus"; 
} 

另一种方法,如果你有大量的批量投资是运行PS脚本作为一个班轮,返回退出代码。


@ECHO off 
SET PS=powershell -nologo -command 
%PS% "& {if((get-service SvcName).Status -eq 'Running'){exit 1}}" 

ECHO.%ERRORLEVEL% 

作为一行代码运行也绕过了默认的PS代码签名策略,以牺牲混乱为代价。要将PS命令放在.ps1文件中并运行,如powershell myCode.ps1,您可能发现签署PowerShell脚本需要以自动方式运行它们(取决于您的环境)。见http://www.hanselman.com/blog/SigningPowerShellScripts.aspx的细节

11

让我们回到老派一批编程在Windows

net start | find "Service Name" 

这将工作无处不在...

2

我建议 WMIC Service WHERE "Name = 'SericeName'" GET Started

WMIC Service WHERE "Name = 'ServiceName'" GET ProcessId(的ProcessID将是零,如果服务未启动)

您可以根据设定的误差水平是否前者返回“TRUE”或后者返回非零

0

只要添加到列表中,如果您使用Powershell。

sc.exe query "ServiceName" | findstr RUNNING 

的命令如下不起作用,因为sc是一个别名设置内容在PowerShell中。

sc query "ServiceName" | findstr RUNNING 

find也不会在PowerShell的一些原因不明我的工作。

sc.exe query "ServiceName" | find RUNNING 
2
sc query "servicename" | findstr STATE

例如:

sc query "wuauserv" | findstr STATE

要报告什么的Windows更新服务是干什么的,运行/暂停等
这也适用于Windows 10以后感谢我。

0
SERVICO.BAT 
@echo off 
echo Servico: %1 
if "%1"=="" goto erro 
sc query %1 | findstr RUNNING 
if %ERRORLEVEL% == 2 goto trouble 
if %ERRORLEVEL% == 1 goto stopped 
if %ERRORLEVEL% == 0 goto started 
echo unknown status 
goto end 
:trouble 
echo trouble 
goto end 
:started 
echo started 
goto end 
:stopped 
echo stopped 
goto end 
:erro 
echo sintaxe: servico NOMESERVICO 
goto end 

:end 
0
@ECHO OFF 
REM testing at cmd : sc query "MSSQLSERVER" | findstr RUNNING 
REM "MSSQLSERVER" is the name of Service for sample 
sc query "MSSQLSERVER" %1 | findstr RUNNING 
if %ERRORLEVEL% == 2 goto trouble 
if %ERRORLEVEL% == 1 goto stopped 
if %ERRORLEVEL% == 0 goto started 
echo unknown status 
goto end 
:trouble 
echo Oh noooo.. trouble mas bro 
goto end 
:started 
echo "SQL Server (MSSQLSERVER)" is started 
goto end 
:stopped 
echo "SQL Server (MSSQLSERVER)" is stopped 
echo Starting service 
net start "MSSQLSERVER" 
goto end 
:erro 
echo Error please check your command.. mas bro 
goto end 

:end 
0

使用Cygwin猛砸有:

sc query "SomeService" |grep -qo RUNNING && echo "SomeService is running." || echo "SomeService is not running!" 

(请确保您在您的PATH有sc.exe

0

我注意到使用find时,没有人提到使用正则表达式/ findstr - 基于答案。对于类似命名的服务,这可能会有问题。

比方说你有两个服务,CDPUserSvcCDPUserSvc_54530

如果您最常使用的find/findstr基于答案的到这里为止,你只CDPUserSvc_54530运行时得到误报为CDPUserSvc查询。

/r/c开关,用于findstr可以帮助我们处理使用情况,以及特殊字符,表示该行的末尾,$

这个查询只能验证CDPUserSvc服务的运行而忽略CDPUserSvc_54530

sc query|findstr /r /c:"CDPUserSvc$"

相关问题