从批处理脚本启动Windows服务,然后根据结果采取适当的措施 [英] Start a Windows service from a batch script and take appropriate action based on result

查看:191
本文介绍了从批处理脚本启动Windows服务,然后根据结果采取适当的措施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.bat脚本,试图最终启动Windows服务.

I have a .bat script that attempts to start a Windows service at the end.

:start_wildfly
echo.
set /p wildfly_service_name="Enter Wildfly service name: "
echo INFO: Starting %wildfly_service_name%...
echo.
call net start "%wildfly_service_name%"

我希望能够解释net start尝试的结果,以便我的脚本可以在失败时采取适当的措施(例如,如果服务已经在运行,请重新启动它.如果服务名称无效) ,请再次提示输入该名称,如果用户没有足够的特权,请退出.)

I want to be able to interpret the result of the net start attempt so that I can have my script take the appropriate action if it fails (e.g. if the service is already running, restart it. If the service name is invalid, re-prompt for the name again, if the user doesn't have sufficient privileges, exit).

问题是NET命令不会返回已记录的

The problem is that the NET command does not return the documented Win32_Service class codes.

它确实在控制台上回显错误,但是:

It does echo errors on the console, however:

The requested service has already been started.

More help is available by typing NET HELPMSG 2182.

请参阅 http://ss64.com/nt/net_service.html 以获得以下列表:错误.

See http://ss64.com/nt/net_service.html for a list of the errors.

不幸的是,在这些错误情况下,errorlevel变量始终为2,所以我不能依靠它.

Unforunately, the errorlevel variable is always 2 in these error cases, so I can't rely on that.

我现在想做的是在NET命令的输出上运行FIND,搜索特定的错误代码并对其执行操作.

What I'm now trying to do is run a FIND on the output of the NET command, searching for specific error codes and act upon them.

net start Wildfly 2>&1 | FIND "2182"
if %errorlevel% equ 0 goto service_already_running

因此,FIND的结果存储在errorlevel中,我可以通过检查errorlevel是否为0来检查FIND是否成功.

So, the result of the FIND is stored in errorlevel and I can check to see if the FIND succeeded by checking if errorlevel is 0. This works.

现在,当我要检查多个错误代码时,问题就来了.例如,我不知道如何扩展上面的代码来检查"2185",并在这种情况下转到另一个标签.

Now, the problem comes when I want to check for more than one error code. I don't know how to expand the code above to check for "2185" as well, for example, and goto a different label in that case.

我现在正尝试将NET命令的整个结果存储到一个变量中,然后对该变量运行FINDSTR.

I'm now attempting to store the entire result of the NET command into a variable, and then run a FINDSTR on that variable.

setlocal EnableDelayedExpansion
set "output_cnt=0"
for /F "delims=" %%f in ('dir /b') do (
    set /a output_cnt+=1
    set "output[!output_cnt!]=%%f"
)
for /L %%n in (1 1 !output_cnt!) DO echo !output[%%n]!

这应该存储并回显输出的每一行,但是最后一行似乎没有任何作用.

This should store and echo each line of the output, however the last line doesn't seem to do anything.

然后我还找到了一些应在变量中搜索并返回是否找到该字符串的代码:

And then I've also found some code that should search within a variable and return whether or not that string was found:

echo.%output%|findstr /C:"2182" >nul 2>&1 && echo Found || echo Not found.

尽管如此,我还是没有运气.我只希望能够解释NET START <SERVICE>的结果并根据结果跳转到某些标签.

I've had no luck putting it all together though. I just want to be able to interpret the result of the NET START <SERVICE> and jump to certain labels based on the result.

推荐答案

我希望能够解释net start尝试的结果

I want to be able to interpret the result of the net start attempt

,以便我可以让脚本在失败时采取适当的措施(例如,如果服务已在运行,请重新启动它.如果服务名称无效,请再次提示输入该名称,如果用户未输入该名称)具有足够的特权,请退出).

so that I can have my script take the appropriate action if it fails (e.g. if the service is already running, restart it. If the service name is invalid, re-prompt for the name again, if the user doesn't have sufficient privileges, exit).

按已启动的方式启动服务:

Start the service as you are already doing:

net start "%wildfly_service_name%"

现在检查服务状态.

有两种方法可以做到这一点.

There are two ways to do this.

  1. 再次使用net start来查看服务是否正在运行:

  1. Use net start again to see if the service is running:

net start | find "%wildfly_service_name%" > nul
if errorlevel 1 echo The service is not running

  • 使用sc(服务控制)检查服务状态:

  • Use sc (Service Control) to check the service status:

    SC query %wildfly_service_name% | find "STATE" | find "STOPPED"
    

    sc query %wildfly_service_name% | find "STATE" | find "RUNNING"
    

    如果找不到文本 ,则以上两个语句将返回%errorlevel% = 1.

    The two statements above will return %errorlevel% = 1 if the text is not found.


    进一步阅读

    • Windows CMD命令行的AZ索引-与Windows cmd行相关的所有内容的绝佳参考.
    • net -NET命令用于管理网络资源.
    • sc -服务控制-创建,启动,停止,查询或删除任何Windows服务.

    • Further Reading

      • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
      • net - The NET Command is used to manage network resources.
      • sc - Service Control - Create, Start, Stop, Query or Delete any Windows SERVICE.
      • 这篇关于从批处理脚本启动Windows服务,然后根据结果采取适当的措施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆