簡體   English   中英

批處理文件以檢查Windows服務並啟動它們

[英]Batch file to check Windows services and Start them

我正在嘗試創建一個批處理文件,該文件將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"

首先,您的第一個服務是services [1],但是循環從0開始。

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

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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM