68

I've got a batch file. After it finished running, i.e. all command lines have been executed, the cmd.exe window stays open. However, I'd like to have it closed right after the batch file finishes its job.

So far I've tried using the exit command within the batch file to close the cmd window (I also have a shortcut on the desktop) but it doesn't seem to work:

tncserver.exe C:\Work -p4 -b57600 -r -cFE -tTNC426B
exit
Jim McAdams
  • 1,034
  • 1
  • 14
  • 30
Blitzcrank
  • 917
  • 2
  • 10
  • 19

11 Answers11

86

It should close automatically, if it doesn't it means that it is stuck on the first command.

In your example it should close either automatically (without the exit) or explicitly with the exit. I think the issue is with the first command you are running not returning properly.

As a work around you can try using

start "" tncserver.exe C:\Work -p4 -b57600 -r -cFE -tTNC426B
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • 4
    hohoho, should be like this: start tncserver.exe C:\Work -p4 -b57600 -r -cFE -tTNC426B – Blitzcrank Jan 31 '13 at 13:12
  • 6
    The "" is used to stop a problem with `start`, if the program you are running has spaces it will interpret the first string as the title for the app. The "" gives it a blank title so the rest will work as expected. – Bali C Jan 31 '13 at 13:14
  • Just wanted to make an addition here, I was running a minecraft server which upon the /restart command would launch the bat script, the old command prompt wouldn't close however, instead it would go back to the command line. using "exit" at the end of the bat script fixed that for me – xorinzor Oct 21 '16 at 11:34
21

Your code is absolutely fine. It just needs "exit 0" for a cleaner exit.

 tncserver.exe C:\Work -p4 -b57600 -r -cFE -tTNC426B
 exit 0
13

If you only need to execute only one command all by itself and no wait needed, you should try "cmd /c", this works for me!

cmd /c start iexplore "http://your/url.html"

cmd /c means executing a command and then exit.

You can learn the functions of your switches by typing in your command prompt

anycmd /?
Jenna Leaf
  • 2,255
  • 21
  • 29
12

I added the start and exit which works. Without both it was not working

start C:/Anaconda3/Library/bin/pyrcc4.exe -py3 {path}/Resourses.qrc -{path}/Resourses_rc.py
exit
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
user5770752
  • 121
  • 1
  • 2
6
%Started Program or Command% | taskkill /F /IM cmd.exe

Example:

notepad.exe | taskkill /F /IM cmd.exe

Unfortunately, if you have other cmd windows open, it kills them as well.

RF1991
  • 2,037
  • 4
  • 8
  • 17
user1448914
  • 99
  • 2
  • 9
5

Used this to start Xming, placed the bat file in the Start->Startup directory and now I have xming running on start up.

start "" "C:\Program Files (x86)\Xming\Xming.exe" -screen 0 -clipboard -multiwindow
Loran
  • 398
  • 2
  • 8
0

For closing cmd window, especially after ending weblogic or JBOSS app servers console with Ctrl+C, I'm using 'call' command instead of 'start' in my batch files. My startWLS.cmd file then looks like:

call [BEA_HOME]\user_projects\domains\test_domain\startWebLogic.cmd

After Ctrl+C(and 'Y' answer) cmd window is automatically closed.

user2590805
  • 410
  • 4
  • 6
0

I came across this question while searching the same topic but for Windows So, if anyone wonders how it can be done here is it

echo | set /p=Your Text

Output:

Your Text

I used it to save data on the clipboard so trimming text was necessary It looked like this

echo | set /p=Text| clip
0

Max Lorens's response, currently the least-voted answer, is the only one that works for my particular script.

I believe for complex scripts that output and have pauses in them such as "timeout /t 4 /nobreak", his idea is best. Simply force-killing cmd.exe can/will mess up systems that are running background processes inside of cmd.exe containers, while that command will only kill visible windows with cmd.exe in the title.

For reference it was:

REM EXECUTE TASK KILL TO CLOSE CMD PROGRAM.-
wmic Path win32_process Where "Caption Like 'cmd%.exe'" Call Terminate

Put it in the last code line of your batch file program.

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
0

On windows11 I'm concatenating commands like this

cmd /c dotnet lsb && npm run build:dev && exit 0

in this example it runs in a single shell the two commands and then closes it.

Not Important
  • 762
  • 6
  • 22
-1
REM EXECUTE TASK KILL TO CLOSE CMD PROGRAM.-
wmic Path win32_process Where "Caption Like 'cmd%.exe'" Call Terminate

PD: PUT IT IN THE LAST CODE LINE OF YOUR BATCH FILE PROGRAM.-