windows - 如何在不杀死其他cmd的情况下杀死一个cmd进程?

标签 windows git batch-file jenkins

我正在处理一个批处理文件,它应该启动一个进程 (CMD),然后它应该在完成后终止该进程。问题是,Imagename 是 cmd.exe,另一个问题是它应该在 Jenkins 上运行。 这是我测试过的:

  • 使用窗口名称通过 wmic 获取 PID 以查找进程 -> 在 Jenkins 失败
  • Taskkill 通过命名窗口-> 失败,因为 Jenkins 没有 由于安全问题显示窗口。
  • Taskkill by imagename -> 失败,因为还有其他 cmd 进程 同时运行
  • 使用 pid 的 Taskkill,但从上一个 cmd 开始的 pid。 - 有效,但它是 不太安全。

我无法理解 wmic 的工作原理,但据我所知,我无法使用 START 命令之类的命令启动进程。

条件:

  • 一段时间后无法终止,因为我需要 mergetool,有时 mergetool 可能会花费太长时间。

  • 它应该与其他 (cmd) 进程同时运行//Jenkins

我的问题是,有没有办法从 START 命令获取 PID?

以下是一些对我帮助很大的问题! Windows batch file : PID of last process? Compare number of a specific process to a number

代码:

set "console_name=cmd.exe"
set "git_command=%gitcmd% mergetool ^>output.txt"
tasklist /FI "imagename eq %console_name%" /NH /FO csv > task-before.txt
START "mergetool_w" CMD /c %git_command%
tasklist /FI "imagename eq %console_name%" /NH /FO csv > task-after.txt
for /f "delims=, tokens=2,*" %%A in ('fc /L /LB1  task-before.txt task-after.txt') do set pid=%%A
pid=!pid:"=!
echo pid is %pid%
TASKKILL /t /pid %pid% /f

最佳答案

你实际上可以使用 findstr根据您的文件 task-before.txttask-after.txt,检查在您的 start 命令行之后添加了哪些任务:

findstr /LXVG:task-before.txt task-after.txt

由于 nasty bug ,这在某些情况下可能会导致意外的输出。为防止这种情况,请添加 /I 选项,如果您可以接受不区分大小写的搜索:

findstr /ILXVG:task-before.txt task-after.txt

关于windows - 如何在不杀死其他cmd的情况下杀死一个cmd进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51637742/

相关文章:

windows - 批处理文件中 for 循环中关系运算符的用法

dos - 我该如何修复这个批处理脚本?

batch-file - 如何检查zip或rar文件是否包含1个或多个文件?

windows - 在使用 SSL 的 Drush 中进行身份验证时遇到问题

windows - Windows 上未显示 Qt5 快速应用程序窗口

在 Windows 上通过管道进行 Java/C++ 通信

git - 如何查看包含 git-staged 更改的文件

git - 撤消将功能分支重新定位到另一个功能分支

git - 如何通过命令 args 更改 `git log --graph` 中的图形线条字符?

batch-file - 如何使用批处理将 dll 作为 exe 执行