使用 Batch 计算流程实例
我试图使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您的要求是否严格要求在批处理文件中完成此操作,但使用 VBS 脚本和 WMI 非常容易。
创建一个扩展名为
vbs
的新文件,并将以下内容添加到该文件中。该脚本将显示一个对话框,其中包含cmd.exe
实例的数量。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 ofcmd.exe
instances.这就是我最终所做的。我使用了 linuxuser27 发布的脚本和一个很好的 FOR 循环来获取存储在变量中的流程实例计数。
这是我发布的 vbscript linuxuser27,我稍微调整了一下,以便我可以传递我想要算作参数的任何进程名称(还删除了 processName 位)。我只是将此称为 proc_count.vbs,正如您在我的批处理文件的源代码中看到的那样。
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.
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.