How to test whether a service is running from the command line

后端 未结 14 1599
天涯浪人
天涯浪人 2020-12-12 18:59

I would like to be able to query whether or not a service is running from a windows batch file. I know I can use:

sc query \"ServiceName\"

14条回答
  •  醉梦人生
    2020-12-12 19:28

    I noticed no one mentioned the use of regular expressions when using find/findstr-based Answers. That can be problematic for similarly named services.

    Lets say you have two services, CDPUserSvc and CDPUserSvc_54530

    If you use most of the find/findstr-based Answers here so far, you'll get false-positives for CDPUserSvc queries when only CDPUserSvc_54530 is running.

    The /r and /c switches for findstr can help us handle that use-case, as well as the special character that indicates the end of the line, $

    This query will only verify the running of the CDPUserSvc service and ignore CDPUserSvc_54530

    sc query|findstr /r /c:"CDPUserSvc$"

提交回复
热议问题