windows 如果服务存在条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1916050/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:59:04  来源:igfitidea点击:

If Service Exists Condition

windowsbatch-filecmd

提问by Burt

How would you check if a WIN32 service exists and, if so, do some operation?

您将如何检查 WIN32 服务是否存在,如果存在,则执行一些操作?

回答by Joey

You can't do this in DOS, as DOS is not Windows and doesn't even have the concept of a "service".

您不能在 DOS 中执行此操作,因为 DOS 不是 Windows,甚至没有“服务”的概念。

In a Windows batch file you can use the sccommand to find services:

在 Windows 批处理文件中,您可以使用以下sc命令查找服务:

sc query | findstr SERVICE_NAME

This will enumerate all services and yield their respective names.

这将枚举所有服务并产生它们各自的名称。

You can search for a specific service with

您可以使用以下命令搜索特定服务

sc query | findstr /C:"SERVICE_NAME: myservice"

Remember that this search is case-sensitive. You can add the /Iswitch to findstrto avoid that.

请记住,此搜索区分大小写。您可以添加/I开关findstr来避免这种情况。

回答by icabod

Off the top of my head, you can check if a specific service is running, as mentioned by bmargulies, using the "net" command, piping the result into "find". Something like the following would check if a service was running, and if so stop it. You can then start it without worrying about if it was already running or not:

在我的脑海中,您可以检查特定服务是否正在运行,如 bmargulies 所述,使用“net”命令,将结果传送到“find”。类似下面的内容将检查服务是否正在运行,如果是,则停止它。然后你可以启动它而不必担心它是否已经在运行:

net start | find "SomeService"
if ERRORLEVEL 1 net stop "SomeService"
net start "SomeService"

If you're using findstr to do a search, as some of the other answers have suggested, then you would check for ERRORLEVEL equal to 0 (zero)... if it is then you have found the string you're looking for:

如果您使用 findstr 进行搜索,正如其他一些答案所建议的那样,那么您将检查 ERRORLEVEL 是否等于 0(零)...如果是,那么您已经找到了您要查找的字符串:

net start | findstr "SomeService"
if ERRORLEVEL 0 net stop "SomeService"
net start "SomeService"

Essentially most DOS commands will set ERRORLEVEL, allowing you to check if something like a find has succeeded.

本质上,大多数 DOS 命令都会设置 ERRORLEVEL,允许您检查诸如 find 之类的操作是否成功。

回答by helios456

Just an addendum to the accepted answer. If you are looking to do other things than just restarting the service, and are looking to see if the service is installed.

只是已接受答案的附录。如果您希望做其他事情而不只是重新启动服务,并且希望查看服务是否已安装。

sc query state= all | findstr /C:"SERVICE_NAME: MyService" 
if ERRORLEVEL 0 (**My Operation**)

In this case, the state= allis important since if the service is not started, it will be interpreted as not installed, which are two separate things.

在这种情况下,state= all很重要,因为如果服务未启动,它将被解释为未安装,这是两个独立的事情。

回答by Maxence

I'm using the code below:

我正在使用下面的代码:

SC QUERY | FIND "SERVICE_NAME: MyService"
IF %ERRORLEVEL% EQU 0 NET STOP MyService

If MyServiceis not found, %ERRORLEVEL% will be set at 1 by FIND, else it will stay at 0. The instruction IF %ERRORLEVEL% EQU 0 allows you to test this last case and proceed with an operation on your service.

如果未找到MyService,FIND 将 %ERRORLEVEL% 设置为 1,否则它将保持为 0。指令 IF %ERRORLEVEL% EQU 0 允许您测试最后一种情况并继续对您的服务进行操作。

IF ERRORLEVEL 0 NET STOP MyService

will not work, because it executes the command if %ERRORLEVEL% is greater or equal to zero.

将不起作用,因为如果 %ERRORLEVEL% 大于或等于 0,它会执行命令。

In a Visual Studio post build event, you have to put an:

在 Visual Studio 后期构建事件中,您必须放置:

EXIT 0

at the end because VS will detect that %ERRORLEVEL% != 0 and it will consider that the post build event has failed. Use this with care because this will hide all the errors in your command sequence.

最后,因为 VS 会检测到 %ERRORLEVEL% != 0 并且它会认为后期构建事件失败了。小心使用它,因为这将隐藏命令序列中的所有错误。

With this trick, you can ignore the error and use this in your post build event to restart your service:

使用这个技巧,您可以忽略错误并在您的后期构建事件中使用它来重新启动您的服务:

NET STOP MyService
NET START MyService
EXIT 0

回答by user2956477

Should not be success tested: "if (not) errorlevel 1" ??

不应该成功测试:“if (not) errorlevel 1” ??

In windows shell "if errorlevel #" means the errorlevel is # or higher, so "if errorlevel 0" is alway true.

在 Windows shell 中,“if errorlevel #”表示错误级别为 # 或更高,因此“if errorlevel 0”始终为真。

回答by Jay Zeng

How about using WMIC:

如何使用WMIC

First list all processes, then grep your process name. No result will be printed if it does not exist.

首先列出所有进程,然后 grep 您的进程名称。如果不存在,则不会打印任何结果。

wmic service |findstr "ProcessName"

Example:

例子:

C:\>wmic service |findstr "Search"
FALSE        TRUE        Windows Search