简体   繁体   English

运行批处理文件后如何关闭命令行窗口?

[英]How to close the command line window after running a batch file?

I've got a batch file.我有一个批处理文件。 After it finished running, ie all command lines have been executed, the cmd.exe window stays open.运行完成后,即所有命令行都已执行, cmd.exe窗口保持打开状态。 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:到目前为止,我已经尝试在批处理文件中使用exit命令来关闭 cmd 窗口(我在桌面上也有一个快捷方式),但它似乎不起作用:

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

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 .在您的示例中,它应该自动关闭(没有exit )或显式关闭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

Your code is absolutely fine.你的代码绝对没问题。 It just needs "exit 0" for a cleaner exit.它只需要“exit 0”即可更干净地退出。

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

I added the start and exit which works.我添加了有效的startexit Without both it was not working没有两者,它就不起作用

start C:/Anaconda3/Library/bin/pyrcc4.exe -py3 {path}/Resourses.qrc -{path}/Resourses_rc.py
exit

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”,这对我有用!

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

cmd /c means executing a command and then exit. cmd /c 表示执行命令然后退出。

You can learn the functions of your switches by typing in your command prompt您可以通过在命令提示符中键入来了解开关的功能

anycmd /?

%Started Program or Command% | %启动程序或命令% | taskkill /F /IM cmd.exe taskkill /F /IM cmd.exe

Example:例子:

notepad.exe |记事本.exe | taskkill /F /IM cmd.exe taskkill /F /IM cmd.exe

Unfortunately, if you have other cmd windows open, it kills them as well.不幸的是,如果您打开了其他 cmd 窗口,它也会杀死它们。

使用它来启动 Xming,将 bat 文件放在 Start->Startup 目录中,现在我在启动时运行了 xming。

start "" "C:\Program Files (x86)\Xming\Xming.exe" -screen 0 -clipboard -multiwindow

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.为了关闭 cmd 窗口,尤其是在使用 Ctrl+C 结束 weblogic 或 JBOSS 应用服务器控制台之后,我在批处理文件中使用了“ call ”命令而不是“ start ”。 My startWLS.cmd file then looks like:我的startWLS.cmd文件看起来像:

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

After Ctrl+C(and 'Y' answer) cmd window is automatically closed.在 Ctrl+C(和 'Y' 回答)后,cmd 窗口会自动关闭。

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我在搜索相同主题时遇到了这个问题,但对于 Windows 所以,如果有人想知道如何在这里完成它

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

Max Lorens's response, currently the least-voted answer, is the only one that works for my particular script. Max Lorens 的回答是目前投票最少的回答,是唯一适用于我的特定脚本的回答。

I believe for complex scripts that output and have pauses in them such as "timeout /t 4 /nobreak", his idea is best.我相信对于输出并在其中暂停的复杂脚本,例如“超时 /t 4 / nobreak”,他的想法是最好的。 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.简单地强制杀死 cmd.exe 可以/会弄乱在 cmd.exe 容器内运行后台进程的系统,而该命令只会杀死标题中带有 cmd.exe 的可见窗口。

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.将它放在批处理文件程序的最后一行代码中。

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.- PD:把它放在你的批处理文件程序的最后一行代码中。-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM