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.
TrueY's answer seemed the most elegant solution, however, I had to do some messing around because I didn't understand what exactly was going on. Let me clear things up to hopefully save some time for the next person.
TrueY's modified Answer:
Anyway, I hope that helps. I know sometimes reading batch/command-line can be kind of confusing sometimes if you're kind of a newbie, like me.
I used the script provided by Matt (2008-10-02). The only thing I had trouble with was that it wouldn't delete the
search.log
file. I expect because I had tocd
to another location to start my program. Icd
'd back to where the BAT file andsearch.log
are, but it still wouldn't delete. So I resolved that by deleting thesearch.log
file first instead of last.Under Windows you can use Windows Management Instrumentation (WMI) to ensure that no apps with the specified command line is launched, for example:
wmic process where (name="nmake.exe") get commandline | findstr /i /c:"/f load.mak" /c:"/f build.mak" > NUL && (echo THE BUILD HAS BEEN STARTED ALREADY! > %ALREADY_STARTED% & exit /b 1)
I don't know how to do so with built in CMD but if you have grep you can try the following:
The answer provided by Matt Lacey works for Windows XP. However, in Windows Server 2003 the line
returns
which is then read as the process is running.
I don't have a heap of batch scripting experience, so my soulution is to then search for the process name in the
search.log
file and pump the results into another file and search that for any output.I hope this helps someone else.
I'm assuming windows here. So, you'll need to use WMI to get that information. Check out The Scripting Guy's archives for a lot of examples on how to use WMI from a script.