Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find out if cmd.exe is running before start

144 views
Skip to first unread message

Diddy Kong

unread,
Oct 26, 2004, 4:36:26 PM10/26/04
to
How to check if there are cmd.exe running before running a script?

If cmd.exe exist it should goto :error

Thanks in advance.


-Diddy

Phil Robyn

unread,
Oct 26, 2004, 5:08:14 PM10/26/04
to
Diddy Kong wrote:


tlist | findstr /i "cmd.exe" >nul && echo It's running.

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

Timo Salmi

unread,
Oct 26, 2004, 8:12:46 PM10/26/04
to
Diddy Kong <didd...@supertel.fi> wrote:
> How to check if there are cmd.exe running before running a script?
> If cmd.exe exist it should goto :error

The same solution applies:

68) How can I test if a program already has been loaded?

103196 Sep 8 2004 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi


In XP one can use Task Manager for the purpose e.g. as follows

@echo off & setlocal enableextensions
tasklist|find "Netscp.exe ">nul
if %errorlevel% EQU 0 (
echo Netscape has been loaded
) ELSE (
echo Netscape has not been loaded)
endlocal & goto :EOF

References/Comments (for NT/2000):
http://www.sysinternals.com/ntw2k/freeware/pstools.shtml

If you are using a script in a command window to call an MS-DOS
program, and you wish to prevent calling that same program
simultaneously from another window/or via shelling (to avoid
datafile or whatever conflicts), you can proceed as shown below. The
script creates a markerfile for the duration of running the script.

@echo off & setlocal enableextensions
if exist %temp%\vpp3d.mrk (
echo Exiting: vpp3d already is running
goto :EOF)
echo Mark vpp3d>%temp%\vpp3d.mrk
::
:: Call whatever program it happens to be
C:\_F\VPP\VPP3D.COM
::
del %temp%\vpp3d.mrk
endlocal & goto :EOF

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

Marvin Kreyer

unread,
Oct 26, 2004, 11:48:03 PM10/26/04
to
I don't have tlist on this box to test this, but wouldn't the execution of
this always yield a positive result because cmd.exe is launched to run it?


"Phil Robyn" <zipp...@berkeley.edu> wrote in message
news:2u7slvF...@uni-berlin.de...

Phil Robyn

unread,
Oct 27, 2004, 12:42:13 AM10/27/04
to
Marvin Kreyer wrote:
> I don't have tlist on this box to test this, but wouldn't the execution of
> this always yield a positive result because cmd.exe is launched to run it?

No, TLIST.EXE (part of the free Resource Kit Support Tools from Microsoft)
runs by itself, not via CMD.EXE.

Phil Robyn

unread,
Oct 27, 2004, 12:58:11 AM10/27/04
to
Marvin Kreyer wrote:

> I don't have tlist on this box to test this, but wouldn't the execution of
> this always yield a positive result because cmd.exe is launched to run it?

OK, I see what you mean. :-) The OP posted to this newsgroup (alt.msdos.batch.nt)
so I provided a *batch* command, viz.

tlist | findstr /i "cmd.exe" >nul && echo It's running.

Even though TLIST.EXE does not require the launching of another instance of CMD.EXE
to execute, at least one copy of CMD.EXE must of course be running in order to execute
the *batch command* that invokes TLIST....

Mark V

unread,
Oct 27, 2004, 1:04:49 AM10/27/04
to
In alt.msdos.batch.nt Marvin Kreyer wrote:

> I don't have tlist on this box to test this, but wouldn't the
> execution of this always yield a positive result because cmd.exe
> is launched to run it?

So what is the end goal? Since process CMD will exist (for the
batch) why/what do you need actually to determine in the batch. More
details may help generate ideas.

Also: pslist.exe (www.sysinternals.com)

Ted Davis

unread,
Oct 27, 2004, 8:59:33 AM10/27/04
to
On 26 Oct 2004 13:36:26 -0700, didd...@supertel.fi (Diddy Kong)
wrote:

>How to check if there are cmd.exe running before running a script?
>
>If cmd.exe exist it should goto :error
>
>Thanks in advance.

If you are testing in a batch file under one of the NT series
operating systems, the answer is always yes because the batch file is
running under CMD, or if the batch is running under COMMAND.COM, an
instance of CMD will be spawned when you launch either TLIST or
PsList.

Note: this is the first time I have seen that observation about CMD
launching under COMMAND to run Windows console applications. I'm not
sure if it is important, except in the context of your question.

What are you *really* trying to do?


T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.

Diddy Kong

unread,
Oct 27, 2004, 4:27:22 PM10/27/04
to
Ted Davis <tda...@gearbox.maem.umr.edu> wrote in message news:<f76vn0l4m8kkthepn...@4ax.com>...


I want to allow my script to be runned only once at time.
To prevent it to be loaded simultaneously the way that Timo described
are not so good. The reason for that is when the script interrupts, it
wont delete that "vpp3d.mrk" file. I need more waterproof solution.

I see that finding out if cmd.exe is running is not so clever because
the script itself uses cmd.exe.

So my question is: Is it possible to see if there are two cmd.exe
running and if there are, then it should quit. Btw.. hopefully this
can be done with Windows XP own command.

-Diddy

Ted Davis

unread,
Oct 27, 2004, 4:55:31 PM10/27/04
to
On 27 Oct 2004 13:27:22 -0700, didd...@supertel.fi (Diddy Kong)
wrote:

You don't really care about CMD, you care about the script - that is
almost trivial.

@echo off
tlist | find "%0" > nul
if not ERRORLEVEL 1 goto :err

:err
error handler goes here

TLIST is supposed to be in this package: <
http://download.microsoft.com/download/win2000platform/utility/1/NT5/EN-US/umdhtools.exe>

but I haven't checked myself.

Ted Davis

unread,
Oct 27, 2004, 8:26:06 PM10/27/04
to
On Wed, 27 Oct 2004 15:55:31 -0500, Ted Davis
<tda...@gearbox.maem.umr.edu> wrote:

> @echo off
> tlist | find "%0" > nul
> if not ERRORLEVEL 1 goto :err
>
> :err
> error handler goes here

That's wrong - that's what I get for trying to cram new code into the
last few minutes of the work day.

This is what I was trying to do:

@echo off
tlist | find "foo" > null
if not ERRORLEVEL 1 goto :EOF
title foo
your code goes here

title %~n0

Replace "foo" with whatever marker you want - it has to be unique to
the program, but not the name of the file.

That looks for the working title in a tlist dump - if it is found, the
program is running and has passed the fourth line. If not, either the
program is not running or is in the first few lines. You need tlist
instead of some of the other task listers because it displays the
window title (which line four changes form the default, and the last
line sets to the name of the program, though the Windows default title
would be better).


--
T.E.D. (tda...@gearbox.maem.umr.edu)

Marvin Kreyer

unread,
Oct 27, 2004, 10:01:09 PM10/27/04
to
Thanks, Phil.

You explained the situation better than I did. So, this wouldn't really
achieve the OP's goal as is--that we need to determine if their is a
*second* instance of cmd.exe running, right?


"Phil Robyn" <zipp...@berkeley.edu> wrote in message

news:2u8o77F...@uni-berlin.de...

Todd Vargo

unread,
Oct 27, 2004, 4:51:10 PM10/27/04
to

"Diddy Kong" <didd...@supertel.fi> wrote in message
news:17cb9262.04102...@posting.google.com...

> How to check if there are cmd.exe running before running a script?
>
> If cmd.exe exist it should goto :error

As with the others, I'm not sure what you are asking either. You could mean
you want to know if an instance of cmd.exe is already running. You could
also mean you want to know if which command processor is currently running.

For the latter, you might examine %COMSPEC% or even try
REM & GOTO :error

--
Todd Vargo (double "L" to reply by email)

Phil Robyn

unread,
Oct 28, 2004, 1:25:22 PM10/28/04
to
Marvin Kreyer wrote:
> Thanks, Phil.
>
> You explained the situation better than I did. So, this wouldn't really
> achieve the OP's goal as is--that we need to determine if their is a
> *second* instance of cmd.exe running, right?

I'm not sure what the OP's goal is....

Diddy Kong

unread,
Oct 28, 2004, 3:12:28 PM10/28/04
to
Ted Davis <tda...@gearbox.maem.umr.edu> wrote in message news:<uqe0o0592p9qindse...@4ax.com>...

> This is what I was trying to do:
>
> @echo off
> tlist | find "foo" > null
> if not ERRORLEVEL 1 goto :EOF
> title foo
> your code goes here
>
> title %~n0
>

Thanks Ted Davis for that working solution. Seems to work like a charm.

I also like to thank the others for different ways to do it.


-Diddy

0 new messages