[英]Can I get actual grunt process from Start-Process, not CMD process?
I'm using 我正在使用
Start-Process grunt -ArgumentList 'serve' -PassThru
and add it to an ArrayList
. 并将其添加到
ArrayList
。 When I view said ArrayList
it shows cmd
process, not actual node.js
process that was launched. 当我查看
ArrayList
它显示的是cmd
进程,而不是启动的实际node.js
进程。 If I try to Stop-Process
it, quite obviously, kills cmd
process, not the running grunt
. 如果我尝试
Stop-Process
,很显然,它会杀死cmd
进程,而不是正在运行的grunt
。 I can locate running grunt
with Get-Process node
. 我可以通过
Get-Process node
找到运行的grunt
。
The problem is I need multiple grunts running at the same time and I want a way to distinguish between them somehow. 问题是我需要同时运行多个咕unt声,并且我想要一种以某种方式区分它们的方法。 Is there any way I can get actual
node
process into PowerShell upon initializing it? 有什么方法可以在初始化时将实际的
node
进程添加到PowerShell中?
This happens because grunt
is launched via grunt.cmd
which is run by CMD
. 发生这种情况是因为
grunt
是通过CMD
运行的grunt.cmd
启动的。 This file launches grunt
in node
. 该文件在
node
启动grunt
。
Here is an example of how to find notepad2.exe launched from a CMD similarly to your example: 这是一个如何查找从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}
Translating this for grunt: 翻译为咕unt咕::
# 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}
grunt
isn't a Win32 executable, it's a javascript file that's run by node.exe
. grunt
不是Win32可执行文件,而是由node.exe
运行的一个javascript文件。 The easiest way to get the instance of node that's running grunt would be to start it yourself: 获取正在运行grunt的节点实例的最简单方法是自己启动它:
$ps = start-process -passthru node -argumentlist "$env:APPDATA\npm\node_modules\grunt\bin\grunt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.