我如何在不进入每个窗口的情况下终止许多生成的进程,并执行CTRL + C?

时间:2019-03-10 13:51:40

标签: batch-file

我的完整问题是:在完成处理后,如何在不进入每个单独窗口的情况下终止生成的进程,并手动对其进行处理[kbd> CTRL + C 我认为是吗?

当前我的批处理文件如下:

pushd "System"
start call "#1"
popd
pushd "System(#02)"
start call "#2"
popd
pushd "System(#03KFLM)"
start call "#3"
popd
pushd "System(#04Toy)"
start call "#4"
popd
pushd "System(#05Toy)"
start call "#5"
popd
pushd "System(#06Toy)"
start call "#6"
popd
pushd "System(#07TFF)"
start call "#7"
popd
pushd "System(#08TFF)"
start call "#8"
popd
pushd "System(#09TFF)"
start call "#9"
popd
pushd "System(#10TFF)"
start call "#10"
popd
pushd "System(#11TFF)"
start call "#11"
popd
pushd "System(#12TFF)"
start call "#12"
popd
pushd "System(#13TFF)"
start call "#13"
popd
pushd "System(#14TFF)"
start call "#14"
popd
pushd "System(#15TFF)"
start call "#15"
popd
pushd "System(#16TFF)"
start call "#16"
popd
pushd "System(#17TFF)"
start call "#17"
popd
pushd "System(#18TFF)"
start call "#18"
popd
pushd "System(#19NFF)"
start call "#19"
popd
pushd "System(#20PLA)"
start call "#20"
popd
pushd "System(#21Testing)"
start call "#21"
popd
pushd "System(#22TMSL)"
start call "#22"
popd
pushd "System(#23TMHL)"
start call "#23"
popd
pushd "System(#24TMXL)"
start call "#24"
popd
pushd "System(#25Portal)"
start call "#25"
popd
pushd "System(#26DSKF)"
start call "#26"
popd
pushd "System(#27AKF)"
start call "#27"
popd
pushd "System(#28PAKF)"
start call "#28"
popd
pushd "System(#29VM)"
start call "#29"
popd
pushd "System(#30DM)"
start call "#30"
popd
pushd "System(#31PureDoom3KF)"
start call "#31"
popd
pushd "System(#32Halloween2011)"
start call "#32"
popd
pushd "System(#33Halloween2011)"
start call "#33"
popd
pushd "System(#34Perthians)"
start call "#34"
popd
pushd "System(#35Perthians)"
start call "#35"
popd
pushd "System(#36Perthians)"
start call "#36"
popd
pushd "System(#37Perthians)"
start call "#37"
popd
pushd "System(#38Perthians)"
start call "#38"
popd
pushd "System(#39Deep)"
start call "#39"
popd
pushd "SumoCtryTagsServer"
start call "start-server"
popd
ServerPage

这是为了启动某些游戏服务器,其后的#前缀和数字是真实的,而不是替代的,并且与这些相对文件夹中的批处理文件有关。 ServerPage是一个python文件,因此如果需要的话,以.py扩展名结尾,但是其余的都是批处理文件(如上所述),以.bat扩展名结尾。

当我想一次启动40台服务器+ 1个脚本时,这很好用,但是当我用完它们之后要退出那些开始的41个进程又该怎么办?

我将进入每个进程的窗口并按 CRTL + C 41次,因为其中有41次,如果决定放弃则可能是82次或更多次您是视觉C ++错误或需要按两次或两次以上才能使其返回的错误……

当然,按右上角的X比简单地 CRTL + C 快,但是会导致突然终止,因此当前所有日志文件进行中将无法正确关闭或保存。使用 CRTL + C ,这使服务器有足够的时间正确保存和关闭可能已打开并安全关闭的所有日志。那就是为什么我必须使用 CRTL + C 方法....

1 个答案:

答案 0 :(得分:0)

方法是使用taskkill,此命令用于终止(/杀死)进程,并结合使用for /L循环来循环编号。

使用以下代码:

for /L %%A IN (1 1 41) do (
    for /F "delims=" %%B IN ('dir /B /AD "System(#*%%A*)"') do (
        pushd "%%B" && (
            start "%%A" "#%%A"
            popd
        ) || (
            echo cannot find folder %%B. Please be sure you are in the correct directory.
        )
    )
)

pause

for /L %%C IN (1 1 41) do taskkill /FI "WINDOWTITLE eq %%C" /F

如果您有图片名称,请执行taskkill /FI "WINDOWTITLE eq %%C" /F /IM imagename.exe

请注意:

  • 用122行代码启动41个单独的进程是完全没有用的。循环执行1-41并开始处理。
  • 不确定start call的需求。您想实现一些特别的目标吗?我删除了它,因为它很少用。
  • start "num" "#num"num是窗口标题; #num是启动过程。

有关使用的命令的更多信息,请输入cmd.exe

  • for /? 也解释了for /L
  • pushd /?
  • start /?
  • call /?
  • pushd /?
  • popd /?
  • taskkill /? 使用说明