【发布时间】:2019-12-06 09:27:47
【问题描述】:
我的目标是杀死(或以某种方式优雅地关闭)由 powershell 脚本启动的子进程,以便在父进程死亡后不会继续运行(通常通过点击脚本结尾或通过崩溃或 ctrl+c 或任何其他方式)。
我尝试了几种方法,但都没有达到预期效果:
# only one line was active at time, all other were commented
start-job -scriptblock { & 'notepad.exe' } # notepad.exe started, script continues to end, notepad.exe keep running
start-job -scriptblock { 'notepad.exe' } # notepad.exe not started, script continues to end
notepad.exe # notepad.exe started, script continues to end, notepad.exe keep running
& notepad.exe # notepad.exe started, script continues to end, notepad.exe keep running
start-Process -passthru -FilePath notepad.exe # notepad.exe started, script continues to end, notepad.exe keep running
# give script some time before ending
Write-Host "Begin of sleep section"
Start-Sleep -Seconds 5
Write-Host "End of sleep section"
【问题讨论】:
标签: powershell process kill spawn resource-cleanup