在特定CPU负载下运行程序的脚本 [英] Script to run a program at a certain CPU load

查看:115
本文介绍了在特定CPU负载下运行程序的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了大约3个小时,使用了类似于以下的多个代码:

I have tried for like 3 hours now, with multiple codes similar to this:

wmic cpu get loadpercentage > Load.txt
findstr "%random:~,1%" Load.txt > Load1.txt
set load=<Load1.txt
if %load%==" 2 7                              " echo yes
pause

但是它们都遇到类似的问题, wmic cpu的输出得到loadpercentage

But they all run in to a similar problem, the output of wmic cpu get loadpercentage:

LoadPercentage
56

该格式不允许将其放入变量中,因此我无法检查任何内容。我希望可以在Windows CMD和/或Powershell中完成它。

The format just doesn't allow it to be put into a variable, so I can't check it for anything. Perferably, I would like it to be done in Windows CMD and/or Powershell.

感谢您的帮助!

:: To find the "GUID" or the codes for each power plan, run the command in CMD "powercfg -list".
set HighPerformanceMode=8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
set PowerSaverMode=a1841308-3541-4fab-bc81-f71556f20b4a

:loop
SETLOCAL ENABLEDELAYEDEXPANSION
SET "load="

FOR /F "usebackq skip=1 tokens=*" %%f IN (`wmic cpu get loadpercentage`) DO (
    IF "!load!" EQU "" (
        set "load=%%~f"
    )
)

if "%load%" geq "65" (
    ping localhost -n 2 >nul
    if "%load%" geq "65" (
        %systemroot%\System32\powercfg.exe /setactive %HighPerformanceMode%
        )
    ) else (
        if "%load%" lss "25" (
            ping localhost -n 2 >nul
            if "%load%" lss "25" (
                %systemroot%\System32\powercfg.exe /setactive %PowerSaverMode%
                )
            )
)

endlocal
ping localhost -n 3 > nul
goto loop

请确保您更改了 HighPerformanceMode PowerSaverMode 具有特定于计算机的电源计划。您可以通过在cmd中执行 powercfg -list 来找到代码。

Make sure you change the HighPerformanceMode and PowerSaverMode to have your computer specific power plans. You can find the codes by doing powercfg -list in cmd.

然后我制作了一个单独的简短脚本,其中有 C:\Load Batch\Load Batch.bat ,但是您必须将其更改为主脚本所在的任何位置。然后,我使用了一个名为 BAT至EXE转换器的程序,并将其置于Ghost模式下,并将新制作的.exe程序放入启动文件夹中。

I then made a separate short script that just has "C:\Load Batch\Load Batch.bat" in it, but you have to change it to wherever the main script is. Then I used a program called "BAT to EXE converter" and put it in Ghost Mode, and put the newly made .exe program into my startup folder.

我不认为我的问题是重复的,链接的问题是关于获取CPU和RAM的使用率,以便查看,而我的问题是关于以脚本形式使用纯文本形式获取负载百分比。我知道它仅测试一个CPU内核,但是随着一个CPU内核的增加,其他内核也很可能具有相似的负载。我已经在该站点上搜索了用于在运行 wmic cpu get loadpercentage 时将 LoadPercentage文本分开的代码,因为我无法通过这种方式将其设置为变量。 / p>

I don't believe that my question is a duplicate, the linked question is about getting CPU and RAM usage for what appears to be just to view it, while my question is about getting the load percentage as a pure text form to be used in a script. I am aware of it only testing for one CPU core, but as one goes up it is very likely that the others have similar loads. I had searched this site for code that would separate the "LoadPercentage" text when wmic cpu get loadpercentage is ran, because I couldn't set it into a variable that way.

推荐答案

问题可能是 wmic 输出为Unicode 。

It is likely that the problem is that wmic output is in Unicode. How about going with PowerShell?

Get-CimInstance -ClassName CIM_Processor | select LoadPercentage

我不确定要运行什么。

cmd shell程序员的典型后备通常是这样的:

The typical fallback of cmd shell programmers is usually something like:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "X="

FOR /F "usebackq skip=1 tokens=*" %%f IN (`wmic cpu get loadpercentage`) DO (
    IF "!X!" EQU "" (
        set "X=%%~f"
    )
)

ECHO X is %X%

编辑:

实际上,每个内核都会发出一个LoadPercentage。您可能想要它们的算术平均值(平均值)。

Actually, there will be a LoadPercentage emitted for each core. You probably want the arithmetic mean (average) of them.

Get-CimInstance -ClassName CIM_Processor |
    Measure-Object -Property LoadPercentage -Average |
    Select Average

在cmd脚本中执行此操作将涉及将LoadPercentage值相加并除以

Doing this in a cmd script would involve summing the LoadPercentage values and dividing by the count of them.

SET /A TOTAL=0
SET /A CORE_COUNT=0

FOR /F "usebackq skip=1" %%t IN (`type NUL ^| wmic /node:"%SERVER_NAME%" cpu get loadpercentage ^| findstr .`) DO (
    IF "%%t" NEQ "" (
        SET /A TOTAL=!TOTAL! + %%t
    )
    SET /A CORE_COUNT=!CORE_COUNT! + 1
)

SET /A AVG_UTILIZATION=%TOTAL% / %CORE_COUNT%

ECHO Number of cores: %CORE_COUNT%
ECHO Total CPU utilisation: %TOTAL%
ECHO Average CPU utilisation: %AVG_UTILIZATION%%%

这篇关于在特定CPU负载下运行程序的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆