Mombu the Microsoft Forum

Mombu the Microsoft Forum > Microsoft > Windows 2003 Server (TECHNET) > How to close cmd.exe from a batch file
REGISTER NOW! Mark Forums Read




Reply
 
Thread Tools Display Modes
1 22nd October 07:59
External User
 
Posts: 1
Default How to close cmd.exe from a batch file



Hello,
I am running a simple batch file that telnets to a host on TCP port 25.
The telnet works fine but after it times out (~2 minutes later), the command
window stays open.

I need to be able to close this window after the telnet times out. Is there
a way to do this? Would you show me how to close this window?

Thanks in advance,
-Steve
  Reply With Quote
SPONSORED LINKS BY GOOGLE

 


2 22nd October 07:59
pegasus \(mvp\)
External User
 
Posts: 1
Default How to close cmd.exe from a batch file



Telnet has a habit of not returning control to the underlying
command process. This means that you need to kill it forcefully.
You can do the whole process (i.e. launch & kill telnet) with
this batch file. If you call it something other than d:\test.bat
then you must adjust that reference on Line 4 in the batch file.

@echo off
if not "%1"=="" goto kill
set delay=120
start /b /min "Telnet Monitor" d:\test.bat %delay%
telnet pegasus 5500 goto :eof :Kill
ping localhost -n %1 >nul
taskkill /im telnet.exe /f

An alternative way is to use NetCat instead of telnet
(http://www.securityfocus.com/data/tools/nc11nt.zip).
  Reply With Quote
3 22nd October 07:59
External User
 
Posts: 1
Default How to close cmd.exe from a batch file


Thanks for the info. The script does kill the Telnet session but I should
have stated that I need the cmd.exe process killed. The Telnet session will
timeout and close on its own, it's the cmd.exe process that I need to close.

Here are the results with the script unchanged:
220 relay01.xyz.com ESMTP Postfix
SUCCESS: The process "telnet.exe" with PID 4252 has been terminated.
C:\Temp>@echo off

That does not quite give me what I need. Since I need the cmd.exe process
killed, I modified the script a bit. I increased the timeout to 305 seconds
since Telnet timesout after 5 minutes, not 2 like I thought. I also changed
the last line to kill cmd.exe.

@echo off
if not "%1"=="" goto kill
set delay=305
start /b /min "Telnet Monitor" c:\Temp\test.bat %delay%
telnet 10.1.240.16 25
goto :eof

:Kill
ping localhost -n %1 >nul
taskkill /im cmd.exe /f


Below are the results from the modified script above. When I run the script,
two cmd.exe's are launched for some reason. Both are killed by the batch file
however, the window is still open. Ideally, I need to close the window as
well. I do notice in Taskmgr that there are no cmd.exe processes running
after the batch completes which is good :-) so having the window stay open
may not be a problem but I can't say for sure.

I needed the cmd's to close because I kept running out of memory after a few
hundred processes were left open over night:-).

Anyway, this works pretty well. If you can think of a way to close these
windows too, that would be ideal. If not, I think this will work out ok.

Thanks for your help!
-Steve


220 relay01.kintera.com ESMTP Postfix
SUCCESS: The process "cmd.exe" with PID 15104 has been terminated.
SUCCESS: The process "cmd.exe" with PID 5772 has been terminated.
  Reply With Quote
4 22nd October 07:59
pegasus \(mvp\)
External User
 
Posts: 1
Default How to close cmd.exe from a batch file


When you invoke a batch file via the Start/Run box or via a
shortcut then its associated command processor closes by
itself when the batch file runs out of lines.

If you invoke a batch file from a Command Prompt then
you must insert an "exit" command at the end of the batch
file in order to close the Command Prompt.
  Reply With Quote
5 22nd October 07:59
john fullbright
External User
 
Posts: 1
Default How to close cmd.exe from a batch file


EXIT
  Reply With Quote
SPONSORED LINKS BY GOOGLE

 


Reply


Thread Tools
Display Modes







Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
Also visit Ogoun the Usenet Archive
666