windows 如何从命令行测试服务是否正在运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/353161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test whether a service is running from the command line
提问by Scott Langham
I would like to be able to query whether or not a service is running from a windows batch file. I know I can use:
我希望能够从 Windows 批处理文件查询服务是否正在运行。我知道我可以使用:
sc query "ServiceName"
sc 查询“服务名称”
but, this dumps out some text. What I really want is for it to set the errorlevel
environment variable so that I can take action on that.
但是,这会转储一些文本。我真正想要的是它设置errorlevel
环境变量,以便我可以对此采取行动。
Do you know a simple way I can do this?
你知道我可以做到这一点的简单方法吗?
UPDATE
Thanks for the answers so far. I'm worried the solutions that parse the text may not work on non English operating systems. Does anybody know a way around this, or am I going to have to bite the bullet and write a console program to get this right.
更新
感谢您到目前为止的答案。我担心解析文本的解决方案可能无法在非英语操作系统上运行。有没有人知道解决这个问题的方法,或者我将不得不硬着头皮编写一个控制台程序来解决这个问题。
回答by Igal Serban
sc query "ServiceName" | find "RUNNING"
回答by Shahin
Let's go back to the old school of batch programing on windows
让我们回到 Windows 上批处理编程的老派
net start | find "Service Name"
This will work everywhere...
这将在任何地方工作......
回答by Shahin
if you don't mind to combine the net command with grep you can use the following script.
如果您不介意将 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
回答by NicJ
回答by Muhammad Imron
@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
回答by Muhammad Imron
Thinking a little bit outside the box here I'm going to propose that powershell may be an answer on up-to-date XP/2003 machines and certainly on Vista/2008 and newer (instead of .bat/.cmd). Anyone who has some Perl in their background should feel at-home pretty quickly.
在这里我想在盒子外面稍微思考一下,我将建议 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";
}
Another way, if you have significant investment in batch is to run the PS script as a one-liner, returning an exit code.
另一种方式,如果您对批处理有大量投资,则将 PS 脚本作为单行运行,返回退出代码。
@ECHO off
SET PS=powershell -nologo -command
%PS% "& {if((get-service SvcName).Status -eq 'Running'){exit 1}}"
ECHO.%ERRORLEVEL%
Running as a one-liner also gets around the default PS code signing policy at the expense of messiness. To put the PS commands in a .ps1 file and run like powershell myCode.ps1
you may find signing your powershell scripts is neccessary to run them in an automated way (depends on your environment). See http://www.hanselman.com/blog/SigningPowerShellScripts.aspxfor details
作为单行模式运行也会以混乱为代价绕过默认的 PS 代码签名策略。将 PS 命令放在 .ps1 文件中并像powershell myCode.ps1
您可能会发现签署您的 powershell 脚本一样运行以自动方式运行它们(取决于您的环境)。有关详细信息,请参阅http://www.hanselman.com/blog/SigningPowerShellScripts.aspx
回答by Mark Sowul
I would suggest
WMIC Service WHERE "Name = 'SericeName'" GET Started
我会建议
WMIC Service WHERE "Name = 'SericeName'" GET Started
or WMIC Service WHERE "Name = 'ServiceName'" GET ProcessId
(ProcessId will be zero if service isn't started)
或WMIC Service WHERE "Name = 'ServiceName'" GET ProcessId
(如果服务未启动,ProcessId 将为零)
You can set the error level based on whether the former returns "TRUE" or the latter returns nonzero
您可以根据前者返回“TRUE”还是后者返回非零来设置错误级别
回答by Galwegian
Try
尝试
sc query state= all
for a list of services and whether they are running or not.
获取服务列表以及它们是否正在运行。
回答by Quack
sc query "servicename" | findstr STATE
for example:
例如:
sc query "wuauserv" | findstr STATE
To report what the Windows update service is doing, running/paused etc.
This is also for Windows 10. Thank me later.
报告 Windows 更新服务正在做什么、运行/暂停等。
这也适用于 Windows 10。稍后感谢我。
回答by Scott Langham
I've found this:
我发现了这个:
sc query "ServiceName" | findstr RUNNING
seems to do roughly the right thing. But, I'm worried that's not generalized enough to work on non-english operating systems.
似乎做了大致正确的事情。但是,我担心这不够通用,无法在非英语操作系统上工作。