How can I check if an application is running from a batch (well cmd) file?
I need to not launch another instance if a program is already running. (I can't change the app to make it single instance only.)
Also the application could be running as any user.
Another possibility I came up with, inspired by using grep, is:
It doesn't need to save an extra file, so I prefer this method.
Just mentioning, if your task name is really long then it won't appear in its entirety in the
tasklist
result, so it might be safer (other than localization) to check for the opposite.Variation of this answer:
Building on vtrz's answer and Samuel Renkert's answer on an other topic, I came up with the following script that only runs
%EXEC_CMD%
if it isn't already running:As was said before, this requires administrative privileges.
I like the
WMIC
andTASKLIST
tools but they are not available in home/basic editions of windows.Another way is to useQPROCESS
command available on almost every windows machine (for the ones that have terminal services - I think only win XP without SP2 , so practialy every windows machine):QPROCESS
command is not so powerful asTASKLIST
and is limited in showing only 12 symbols of process name but should be taken into consideration ifTASKLIST
is not available.More simple usage where it uses the name if the process as an argument (the
.exe
suffix is mandatory in this case where you pass the executable name):The difference between two ways of
QPROCESS
usage is that theQPROCESS *
will list all processes whileQPROCESS some.exe
will filter only the processes for the current user.Using
WMI
objects through windows script host exe instead ofWMIC
is also an option.It should on run also on every windows machine (excluding the ones where the WSH is turned off but this is a rare case).Here bat file that lists all processes through WMI classes and can be used instead ofQPROCESS
in the script above (it is a jscript/bat hybrid and should be saved as.bat
):And a modification that will check if a process is running:
The two options could be used on machines that have no
TASKLIST
.The ultimate technique is using
MSHTA
. This will run on every windows machine from XP and above and does not depend on windows script host settings. the call ofMSHTA
could reduce a little bit the performance though (again should be saved as bat):I like Chaosmaster's solution! But I looked for a solution which does not start another external program (like find.exe or findstr.exe). So I added the idea from Matt Lacey's solution, which creates an also avoidable temp file. At the end I could find a fairly simple solution, so I share it...
This is working for me nicely...