Inside a batch file, how can I tell whether a process is running?

后端 未结 2 1932
后悔当初
后悔当初 2021-01-02 14:21

I\'d like to write a batch file that checks to see if a process is running, and takes one action if it is, and another action if it isn\'t.

I know I can use tasklist

相关标签:
2条回答
  • 2021-01-02 14:56

    You can use "for /f" construct to analyze program output.

    set running=0
    for /f "usebackq" %%T in (`tasklist /nh /fi "imagename eq firefox.exe"`) do set running=1
    

    Also, it's a good idea to stick a

    setlocal EnableExtensions
    

    at the begginning of your script, just in case if the user has it disabled by default.

    0 讨论(0)
  • 2021-01-02 15:02

    Some options:

    • PsList from Microsoft

    http://www.windowsdevcenter.com/pub/a/oreilly/windows/news/win2kcommands_0401.html#ps

    • Cygwin (for ps)
    • the resource kit (for ps.vbs)

    http://www.windowsdevcenter.com/pub/a/oreilly/windows/news/win2kcommands_0401.html#ps

    • Powershell for get-process.
    0 讨论(0)
提交回复
热议问题