批处理文件,根据CPU使用情况打开文件(Batch file to open a file based on the CPU usage)

编程入门 行业动态 更新时间:2024-10-09 01:20:01
批处理文件,根据CPU使用情况打开文件(Batch file to open a file based on the CPU usage) @Echo OFF SET "Caminho=SomePath" SET /A "UsoMaximo=95" SET /A "Intervalo=3" :LOOP cls For /F %%P in ('wmic cpu get loadpercentage ^| FINDSTR "[0-9]"') do ( IF %%P GTR %UsoMaximo% ( start %Caminho% ) ) Ping -n %Intervalo% Localhost >NUL GOTO :LOOP

我在这个问题的代码中创建了这个代码(几乎克隆): 基于进程的CPU重新启动特定服务的批处理文件 。

基本上,当CPU使用率高于95%时,它将打开位于“Caminho”的文件。

问题是我不希望它在CPU使用率高于95%时打开,我希望它在CPU使用率低于95%时打开。

我怎样才能做到这一点? 我需要改变什么?

@Echo OFF SET "Caminho=SomePath" SET /A "UsoMaximo=95" SET /A "Intervalo=3" :LOOP cls For /F %%P in ('wmic cpu get loadpercentage ^| FINDSTR "[0-9]"') do ( IF %%P GTR %UsoMaximo% ( start %Caminho% ) ) Ping -n %Intervalo% Localhost >NUL GOTO :LOOP

I made this code based (almost cloned) on the code in this question: Batch file to restart a specific service based on the CPU of a process.

Basically, it will open a file located in "Caminho" when the CPU usage is higher than 95%.

The problem is i don't want it to open when the CPU usage is higher than 95%, i want it to open when the CPU usage is LOWER than 95%.

How can i do this? What i need to change?

最满意答案

将GTR更改为LSS 。

IF %%P GTR %UsoMaximo% (

从if /? :

Performs conditional processing in batch programs. IF [NOT] ERRORLEVEL number command IF [NOT] string1==string2 command IF [NOT] EXIST filename command NOT Specifies that Windows should carry out the command only if the condition is false. ERRORLEVEL number Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified. string1==string2 Specifies a true condition if the specified text strings match. EXIST filename Specifies a true condition if the specified filename exists. command Specifies the command to carry out if the condition is met. Command can be followed by ELSE command which will execute the command after the ELSE keyword if the specified condition is FALSE The ELSE clause must occur on the same line as the command after the IF. For example: IF EXIST filename. ( del filename. ) ELSE ( echo filename. missing. ) The following would NOT work because the del command needs to be terminated by a newline: IF EXIST filename. del filename. ELSE echo filename. missing Nor would the following work, since the ELSE command must be on the same line as the end of the IF command: IF EXIST filename. del filename. ELSE echo filename. missing The following would work if you want it all on one line: IF EXIST filename. (del filename.) ELSE echo filename. missing If Command Extensions are enabled IF changes as follows: IF [/I] string1 compare-op string2 command IF CMDEXTVERSION number command IF DEFINED variable command where compare-op may be one of: EQU - equal NEQ - not equal LSS - less than LEQ - less than or equal GTR - greater than GEQ - greater than or equal and the /I switch, if specified, says to do case insensitive string compares. The /I switch can also be used on the string1==string2 form of IF. These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the strings are converted to numbers and a numeric comparison is performed. The CMDEXTVERSION conditional works just like ERRORLEVEL, except it is comparing against an internal version number associated with the Command Extensions. The first version is 1. It will be incremented by one when significant enhancements are added to the Command Extensions. CMDEXTVERSION conditional is never true when Command Extensions are disabled. The DEFINED conditional works just like EXIST except it takes an environment variable name and returns true if the environment variable is defined. %ERRORLEVEL% will expand into a string representation of the current value of ERRORLEVEL, provided that there is not already an environment variable with the name ERRORLEVEL, in which case you will get its value instead. After running a program, the following illustrates ERRORLEVEL use: goto answer%ERRORLEVEL% :answer0 echo Program had return code 0 :answer1 echo Program had return code 1 You can also use numerical comparisons above: IF %ERRORLEVEL% LEQ 1 goto okay %CMDCMDLINE% will expand into the original command line passed to CMD.EXE prior to any processing by CMD.EXE, provided that there is not already an environment variable with the name CMDCMDLINE, in which case you will get its value instead. %CMDEXTVERSION% will expand into a string representation of the current value of CMDEXTVERSION, provided that there is not already an environment variable with the name CMDEXTVERSION, in which case you will get its value instead.

Change GTR to LSS.

IF %%P GTR %UsoMaximo% (

From if /?:

Performs conditional processing in batch programs. IF [NOT] ERRORLEVEL number command IF [NOT] string1==string2 command IF [NOT] EXIST filename command NOT Specifies that Windows should carry out the command only if the condition is false. ERRORLEVEL number Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified. string1==string2 Specifies a true condition if the specified text strings match. EXIST filename Specifies a true condition if the specified filename exists. command Specifies the command to carry out if the condition is met. Command can be followed by ELSE command which will execute the command after the ELSE keyword if the specified condition is FALSE The ELSE clause must occur on the same line as the command after the IF. For example: IF EXIST filename. ( del filename. ) ELSE ( echo filename. missing. ) The following would NOT work because the del command needs to be terminated by a newline: IF EXIST filename. del filename. ELSE echo filename. missing Nor would the following work, since the ELSE command must be on the same line as the end of the IF command: IF EXIST filename. del filename. ELSE echo filename. missing The following would work if you want it all on one line: IF EXIST filename. (del filename.) ELSE echo filename. missing If Command Extensions are enabled IF changes as follows: IF [/I] string1 compare-op string2 command IF CMDEXTVERSION number command IF DEFINED variable command where compare-op may be one of: EQU - equal NEQ - not equal LSS - less than LEQ - less than or equal GTR - greater than GEQ - greater than or equal and the /I switch, if specified, says to do case insensitive string compares. The /I switch can also be used on the string1==string2 form of IF. These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the strings are converted to numbers and a numeric comparison is performed. The CMDEXTVERSION conditional works just like ERRORLEVEL, except it is comparing against an internal version number associated with the Command Extensions. The first version is 1. It will be incremented by one when significant enhancements are added to the Command Extensions. CMDEXTVERSION conditional is never true when Command Extensions are disabled. The DEFINED conditional works just like EXIST except it takes an environment variable name and returns true if the environment variable is defined. %ERRORLEVEL% will expand into a string representation of the current value of ERRORLEVEL, provided that there is not already an environment variable with the name ERRORLEVEL, in which case you will get its value instead. After running a program, the following illustrates ERRORLEVEL use: goto answer%ERRORLEVEL% :answer0 echo Program had return code 0 :answer1 echo Program had return code 1 You can also use numerical comparisons above: IF %ERRORLEVEL% LEQ 1 goto okay %CMDCMDLINE% will expand into the original command line passed to CMD.EXE prior to any processing by CMD.EXE, provided that there is not already an environment variable with the name CMDCMDLINE, in which case you will get its value instead. %CMDEXTVERSION% will expand into a string representation of the current value of CMDEXTVERSION, provided that there is not already an environment variable with the name CMDEXTVERSION, in which case you will get its value instead.

更多推荐

本文发布于:2023-07-19 23:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1188563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批处理文件   情况   文件   CPU   Batch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!