【发布时间】:2016-06-07 07:02:21
【问题描述】:
在 Windows 7 桌面中使用 Powershell 2.0
我想创建一个进程来运行一个 ant 命令,然后等待该进程在几分钟内完成。如果超时并且进程仍在运行,那么我想杀死它。
我写了一些代码如下:
$p = Start-Process "cmd.exe" '/c ant execute' -PassThru -RedirectStandardOutput $log_file_path
Wait-Process -id $p.ID -timeout 100
if(!$p.hasExited) {
echo "kill the process"
$p.Kill()
#Stop-Process $p.ID
}
Start-Sleep -s 30
# continue to do other things
虽然进程没有被杀死,但即使在 Start-Sleep 语句被执行后它仍然在运行。
我也尝试过使用 Stop-Process 命令,正如注释掉的行所示,不走运,进程仍在运行。
我可能错过了一些重要的事情,请提供线索。
编辑:
原来cmd窗口中还有一些子进程在运行,仅仅杀死cmd进程是不够的。
我终于用下面的代码完成了工作:
if(!$p.hasExited) {
echo "kill the process"
taskkill /T /F /PID $p.ID
#$p.Kill()
#Stop-Process $p.ID
}
【问题讨论】:
标签: windows powershell