使用 Batch 计算流程实例

发布于 2024-10-10 05:04:31 字数 623 浏览 0 评论 0原文

我试图使用“tasklist”和 grep 对于Windows。我想避免写入任何临时文件。

call set _proc_cnt = tasklist /fi "Imagename eq php-cgi.exe" /nh /fo CSV| grep -c -e "php-cgi"
echo %_proc_cnt%
pause

这是我运行时得到的结果

C:\Users\gm\Desktop>call set _proc_cnt = tasklist /fi "Imagename eq php-cgi.exe" /nh /fo CSV  | grep -c -e "php-cgi"
0

C:\Users\gm\Desktop>echo
ECHO is on.

C:\Users\gm\Desktop>pause
Press any key to continue . . .

有人对为什么不起作用有任何提示吗?

Im trying to count the number of php-cgi.exe processes on my server 2003 system using "tasklist" and grep for windows. I would like to avoid writing to any temp files.

call set _proc_cnt = tasklist /fi "Imagename eq php-cgi.exe" /nh /fo CSV| grep -c -e "php-cgi"
echo %_proc_cnt%
pause

Heres what I get when I run that

C:\Users\gm\Desktop>call set _proc_cnt = tasklist /fi "Imagename eq php-cgi.exe" /nh /fo CSV  | grep -c -e "php-cgi"
0

C:\Users\gm\Desktop>echo
ECHO is on.

C:\Users\gm\Desktop>pause
Press any key to continue . . .

Does anyone have any tips on why that doesnt work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-10-17 05:04:31

我不确定您的要求是否严格要求在批处理文件中完成此操作,但使用 VBS 脚本和 WMI 非常容易。

创建一个扩展名为 vbs 的新文件,并将以下内容添加到该文件中。该脚本将显示一个对话框,其中包含 cmd.exe 实例的数量。

Option Explicit
Dim objWMIService, processItems, processName

processName = "cmd.exe"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set processItems = objWMIService.ExecQuery("Select * from Win32_Process where Name='"  & processName & "'")

Wscript.Echo processName & ": " & processItems.Count

I am not sure if your requirements are strict on this being done in a batch file, but this is pretty easy using a VBS script and WMI.

Create a new file with a vbs extension and add the below to the file. The script will present a dialog with the number of cmd.exe instances.

Option Explicit
Dim objWMIService, processItems, processName

processName = "cmd.exe"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set processItems = objWMIService.ExecQuery("Select * from Win32_Process where Name='"  & processName & "'")

Wscript.Echo processName & ": " & processItems.Count
少女净妖师 2024-10-17 05:04:31

这就是我最终所做的。我使用了 linuxuser27 发布的脚本和一个很好的 FOR 循环来获取存储在变量中的流程实例计数。

FOR /F "tokens=*" %%i IN ('%~dp0count_proc.vbs php-cgi.exe') DO SET _PROC_COUNT=%%i
ECHO %_PROC_COUNT%
PAUSE

这是我发布的 vbscript linuxuser27,我稍微调整了一下,以便我可以传递我想要算作参数的任何进程名称(还删除了 processName 位)。我只是将此称为 proc_count.vbs,正如您在我的批处理文件的源代码中看到的那样。

Option Explicit
Dim objWMIService, processItems, processName

processName = Wscript.Arguments(0)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set processItems = objWMIService.ExecQuery("Select * from Win32_Process where Name='"  & processName & "'")

Wscript.Echo processItems.Count

Heres what I ended up doing. I used the script linuxuser27 posted and a nice FOR loop to get a process instance count stored in a variable.

FOR /F "tokens=*" %%i IN ('%~dp0count_proc.vbs php-cgi.exe') DO SET _PROC_COUNT=%%i
ECHO %_PROC_COUNT%
PAUSE

And here is the vbscript linuxuser27 posted that I tweaked just a little so that I could pass in whatever process name I wanted to count as a parameter (also removed the processName bit). I simply called this proc_count.vbs as you can see in the source for my batch file.

Option Explicit
Dim objWMIService, processItems, processName

processName = Wscript.Arguments(0)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set processItems = objWMIService.ExecQuery("Select * from Win32_Process where Name='"  & processName & "'")

Wscript.Echo processItems.Count
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文