【问题标题】:Batch file to check Windows services and Start them用于检查 Windows 服务并启动它们的批处理文件
【发布时间】:2014-11-15 03:05:04
【问题描述】:

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

我所拥有的似乎并不正常,并且似乎与 set 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"

【问题讨论】:

  • 您忘记添加问题了!

标签: windows batch-file service


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    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.
    

    .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      相关资源
      最近更新 更多