Discussion:
Detecting a Running Application
(too old to reply)
David E. Ross
2021-09-27 19:34:55 UTC
Permalink
Is there a command line method for detecting whether a given application
is running? How about detecting an application's PID (process ID) and
then checking to see if that PID is still running?
--
David E. Ross
"A Message to Those Who Are Not Vaccinated"
See my <http://www.rossde.com/index.html#vaccine>.
DanS
2021-09-27 20:03:55 UTC
Permalink
Post by David E. Ross
Is there a command line method for detecting whether a
given application is running? How about detecting an
application's PID (process ID) and then checking to see if
that PID is still running?
wmic process brief will list all processes w/IDs.

the cmd.exe 'batch' language is super sucky though, for parsing command line
responses.

How you'd get further than than that, as in monitoring a specfifc process, I don't know
(using the CMD 'batch' 'language').
Stan Brown
2021-09-27 20:52:15 UTC
Permalink
Post by DanS
Post by David E. Ross
Is there a command line method for detecting whether a
given application is running? How about detecting an
application's PID (process ID) and then checking to see if
that PID is still running?
wmic process brief will list all processes w/IDs.
I get "ERROR: / Description = Invalid query". I think you meant

wmic process list brief
Post by DanS
the cmd.exe 'batch' language is super sucky though, for parsing command line
responses.
How you'd get further than than that, as in monitoring a specfifc process, I don't know
(using the CMD 'batch' 'language').
There's a plain-text find command in Win 7:

wmic process list brief | find /i "gravity"

Administrative privilege is not needed, even to list a system
process.
--
Stan Brown, Tehachapi, California, USA https://BrownMath.com/
https://OakRoadSystems.com/
Shikata ga nai...
DanS
2021-10-08 20:26:38 UTC
Permalink
Post by Stan Brown
Post by DanS
Post by David E. Ross
Is there a command line method for detecting whether a
given application is running? How about detecting an
application's PID (process ID) and then checking to see
if that PID is still running?
wmic process brief will list all processes w/IDs.
I get "ERROR: / Description = Invalid query". I think you
meant
wmic process list brief
Yes, you are correct...

I. Was not.

Zaidy036
2021-09-27 21:54:32 UTC
Permalink
Post by David E. Ross
Is there a command line method for detecting whether a given application
is running? How about detecting an application's PID (process ID) and
then checking to see if that PID is still running?
use the following as example but check for exact name when it is running
by using TASKLIST in a CMD window:


TASKLIST | FIND "EXCEL" > NUL || GOTO <if EXCEL not running go here>
<do something because EXCEL is running>
David E. Ross
2021-09-30 17:30:00 UTC
Permalink
Post by Zaidy036
Post by David E. Ross
Is there a command line method for detecting whether a given application
is running? How about detecting an application's PID (process ID) and
then checking to see if that PID is still running?
use the following as example but check for exact name when it is running
TASKLIST | FIND "EXCEL" > NUL || GOTO <if EXCEL not running go here>
<do something because EXCEL is running>
I understand what pipe (|) does. But what does double pipe (||) do?
--
David E. Ross
"A Message to Those Who Are Not Vaccinated"
See my <http://www.rossde.com/index.html#vaccine>.
Zaidy036
2021-09-30 19:22:59 UTC
Permalink
Post by David E. Ross
Post by Zaidy036
Post by David E. Ross
Is there a command line method for detecting whether a given application
is running? How about detecting an application's PID (process ID) and
then checking to see if that PID is still running?
use the following as example but check for exact name when it is running
TASKLIST | FIND "EXCEL" > NUL || GOTO <if EXCEL not running go here>
<do something because EXCEL is running>
I understand what pipe (|) does. But what does double pipe (||) do?
&& executes this command only if previous command's errorlevel is 0
or successful

|| executes this command only if previous command's errorlevel is NOT
0 or failed.

command1 && command2 || command3 May be used
Zaidy036
2021-09-30 19:30:59 UTC
Permalink
Post by Zaidy036
Post by David E. Ross
Is there a command line method for detecting whether a given application
is running?  How about detecting an application's PID (process ID) and
then checking to see if that PID is still running?
use the following as example but check for exact name when it is running
TASKLIST | FIND "EXCEL" > NUL || GOTO <if EXCEL not running go here>
<do something because EXCEL is running>
I understand what pipe (|) does.  But what does double pipe (||) do?
&&    executes this command only if previous command's errorlevel is 0
or successful
||    executes this command only if previous command's errorlevel is NOT
0 or failed.
      command1 && command2 || command3 May be used
from https://ss64.com/nt/syntax-redirection.html
Mayayana
2021-09-30 21:41:52 UTC
Permalink
"David E. Ross" <***@not_there.invalid> wrote
| Is there a command line method for detecting whether a given application
| is running? How about detecting an application's PID (process ID) and
| then checking to see if that PID is still running?
|
Does it need to be command line? Sysinternals
process explorer will give you both.
David E. Ross
2021-09-30 22:17:27 UTC
Permalink
Post by Mayayana
| Is there a command line method for detecting whether a given application
| is running? How about detecting an application's PID (process ID) and
| then checking to see if that PID is still running?
|
Does it need to be command line? Sysinternals
process explorer will give you both.
I wanted a command line method to incorporated into a DOS-type script.
--
David E. Ross
"A Message to Those Who Are Not Vaccinated"
See my <http://www.rossde.com/index.html#vaccine>.
Mayayana
2021-10-01 01:31:31 UTC
Permalink
"David E. Ross" <***@not_there.invalid> wrote

| > Does it need to be command line? Sysinternals
| > process explorer will give you both.
|
| I wanted a command line method to incorporated into a DOS-type script.
|

I see. I've done a lot with WMI but I'm not familiar with
the commandline version, WMIC. In case it's of any use,
here's a short VBScript version that you might be able to
adapt to WMIC. It enumerates items in the Win32_Process
class. The list of available properties is vast but mostly useless.
(See the WMI help file.) There are also methods, like Terminate.
This sample just gets EXE path and PID. I've left an error trap
in the code because using WMI depends on 2 services --
Windows Management Instrumentation and DCOM Server Process
Launcher. If those are disabled WMI will not be accessible.
They could be disabled in some corporate settings. (I'd
disable them myself if it were not for the fact that I use
WMI. Aside from cheap system utilities, there's rarely any
reason for anyone to need WMI running and it's a potential
security risk. As is Power Shell.)

You might be able to run this via cscript, or run it and
return the data to your commandline, or maybe run it in
batch (via Power Shell?) if you can figure out the corresponding
syntax for that. But as I say, I don't have any experience
with those options. If you paste the following into Notepad
and save as a .vbs file you can see how it works.

'----begin VBS

On Error Resume Next
Dim WMI, S2, Col
Set WMI = GetObject("WinMgmts:")

If (Err.number <> 0) Then
MsgBox "Error creating WMI object. Error: " & Err.Number & " - " &
Err.Description
WScript.quit
End If

Set Col = WMI.ExecQuery("Select * from Win32_Process")
S2 = S2 & " Process Info:" & vbCrLf & vbCrLf
For Each Ob in Col
S2 = S2 & "Caption: " & Ob.Caption & vbCrLf
S2 = S2 & "ExecutablePath: " & Ob.ExecutablePath & vbCrLf
S2 = S2 & "ProcessID: " & Ob.ProcessID & vbCrLf &
"__________________________" & vbCrLf & vbCrLf
Next


Set Col = Nothing
Set WMI = Nothing

MsgBox S2
Apd
2021-10-02 11:48:32 UTC
Permalink
Post by David E. Ross
Post by Mayayana
| Is there a command line method for detecting whether a given application
| is running? How about detecting an application's PID (process ID) and
| then checking to see if that PID is still running?
Does it need to be command line? Sysinternals
process explorer will give you both.
I wanted a command line method to incorporated into a DOS-type script.
Sysinternals has a package of command line tools called PsTools. In
there is PsList which will list all processes, or individually by mame
or PID.
Loading...