用于检查Windows服务并启动它们的批处理文件

时间:2014-09-20 18:52:00

标签: windows batch-file service

我正在尝试创建一个批处理文件,将7个Windows服务排序到一个列表中,然后逐个检查它们是否正在运行,如果它们不是,则启动它们。

我所拥有的东西似乎并没有被唤醒,似乎回应了集i = o。我试图找出如何正确执行两个for循环,如果有人有任何建议的语法将是真棒

我能够创建一个非常原始的版本,但想要了解更多关于批处理文件"编程"。这是我到目前为止所提出的:

    ::Enter in CC number
    set /p CC=Enter The Site's CC:

    @echo off
    setlocal EnableDelayedExpansion

    ::Create vector with names of services
    set i=0
    for %%s in 
    ("Apache Tomcat"
        "OracleServicePD"
        "OracleXETNSListener_bqw"
        "System Audit Service"
        "RPOS ScemComms Service"
        "RPOS debit credit service"
        "RPOS Remote Device Service"
        "RPOS Messaging Service"
        ) do (
        set /A i=i+1
        set services[!i!]=%%s
        )

    ::Check if all services are running, if not go to it's respective net start method
    ::After all is checked, it goes to :check to show services are running  
    set n=0
    :loop
    for /L %%G in (0,1,7) do (
        net start | find !services[%n%]! > nul 2>&1 
        if not "%errorlevel%"=="0"
        set pathname=!services[%n%]!
        set /A n=n+1
        goto %pathname%
        )

    goto check

    :"Apache Tomcat" 
    net start tomcat6
    goto loop

    :"OracleServicePD"
    net start "OracleServicePD%CC%"
    goto loop

    :"OracleXETNSListener_bqw"
    net start "OracleXETNSListener_bqw"
    goto loop

    :"System Audit Service"
    net start "System Audit Service"
    goto loop

    :"RPOS ScemComms Service"
    net start "RPOS ScemComms Service"
    goto loop

    :"RPOS debit credit service"
    net start "RPOS debit credit service"
    goto loop

    :"RPOS Remote Device Service"
    net start "RPOS Remote Device Service"
    goto loop

    :"RPOS Messaging Service"
    net start "RPOS Messaging Service"
    goto loop

    :check

    echo Apache Tomcat && sc query tomcat6 | find "STATE"
    echo OracleServicePD%CC% && sc query "OracleServicePD%CC%" | find "STATE"
    echo OracleXETNSListener_bqw && sc query "OracleXETNSListener_bqw" | find "STATE"
    echo System Audit Service && sc query "System Audit Service" | find "STATE"
    echo RPOS ScemComms Service && sc query "RPOS ScemComms Service" | find "STATE"
    echo RPOS debit credit service && sc query "RPOS debit credit service" | find "STATE"
    echo RPOS Remote Device Service && sc query "RPOS Remote Device Service" | find "STATE"
    echo RPOS Messaging Service && sc query "RPOS Messaging Service" | find "STATE"

2 个答案:

答案 0 :(得分:0)

首先,你的第一个服务是服务[1],但你的循环从0开始。

更重要的是,%n%来自哪里?你的意思是%% G。

答案 1 :(得分:0)

sc start AeLookupSvc&&echo Started||(sc start AeLookupSvc|Findstr /c:"1056"&&Echo Already Running||Echo Error starting service)

然后你的测试模式不是一个好的编程技术。你做并测试它是否有效。

以上执行一项服务并报告是否已经运行,是否已启动,或者是什么错误阻止它启动。全部在一行。

来自MSDos 6.22帮助文件

│The following list shows each exit code and a brief description of its
│meaning:
│
│0
│    The search was completed successfully and at least one match was found.
│
│1
│    The search was completed successfully, but no matches were found.
│
│2
│    The search was not completed successfully. In this case, an error
│    occurred during the search, and FIND cannot report whether any matches
│    were found.
│
│You can use the ERRORLEVEL parameter on the  command line in a batch
│program to process exit codes returned by FIND.

命令行事项列表。

&    seperates commands on a line.

&&    executes this command only if previous command's errorlevel is 0.

||    (not used above) executes this command only if previous command's errorlevel is NOT 0

>    output to a file

>>    append output to a file

<    input from a file

|    output of one command into the input of another command

^    escapes any of the above, including itself, if needed to be passed to a program

"    parameters with spaces must be enclosed in quotes

+ used with copy to concatinate files. E.G. copy file1+file2 newfile

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

%variablename% a inbuilt or user set environmental variable

!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.

%* (%*) the entire command line.

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.