gpt4 book ai didi

node.js - 我可以从 Start-Process 获取实际的 grunt 进程,而不是 CMD 进程吗?

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:35 25 4
gpt4 key购买 nike

我正在使用

Start-Process grunt -ArgumentList 'serve' -PassThru

并将其添加到 ArrayList 中。当我查看所述 ArrayList 时,它显示 cmd 进程,而不是启动的实际 node.js 进程。如果我尝试 Stop-Process 它,很明显,会杀死 cmd 进程,而不是正在运行的 grunt。我可以使用 Get-Process Node 找到正在运行的 grunt

问题是我需要同时运行多个 grunt,并且我想要一种方法来以某种方式区分它们。有什么方法可以在初始化时将实际的 node 进程放入 PowerShell 中?

最佳答案

发生这种情况是因为 grunt 是通过 grunt.cmd 启动的,而 grunt.cmdCMD 运行。此文件在 node 中启动 grunt

下面是如何查找从 CMD 启动的 notepad2.exe 的示例,与您的示例类似:

# start a CMD which starts notepad2 and then waits on it
$process = Start-Process -PassThru 'cmd.exe' '/c "C:\Program Files\Notepad2\Notepad2.exe"'

# Wait for notepad2 to be launched
Start-Sleep -Seconds 2

# find the children of CMD, and kill the one which is notepad2
(Get-CimInstance win32_process -Filter "ParentProcessId='$($process.Id)' AND Name = 'Notepad2.exe'") | %{ Stop-Process -Id $_.ProcessId}

将其翻译为咕噜声:

# start Grunt via grunt.cmd
$process = Start-Process grunt -ArgumentList 'serve' -PassThru

# Wait for node to be launched
Start-Sleep -Seconds 2

# find the children of CMD, and kill the one which is node
(Get-CimInstance win32_process -Filter "ParentProcessId='$($process.Id)' AND Name = 'node.exe'") | %{ Stop-Process -Id $_.ProcessId}

关于node.js - 我可以从 Start-Process 获取实际的 grunt 进程,而不是 CMD 进程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com