Page 1 of 1
Get CPU and Run a EXE
Posted: 24 Jan 2014 05:37
by shashank.kaneria
Hi Folks,
I am making a small DOS batch script.
Scenario: A script which can take the LoadPercentage.
if LoadPercentage is above 90% then it should run an executable(whatever it is).
My findings (just an command): Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage
This will provide me the total cpu utilization(excluding Idle one)
I am stuck at if else loop and comparison operator.
Now we have to put an condition if CPU is more than 90 than execute "Test.EXE"
I will appreciate if anybody helps me out.
Re: Get CPU and Run a EXE
Posted: 24 Jan 2014 05:49
by npocmaka_
rather you'll need logman with alerts
Re: Get CPU and Run a EXE
Posted: 24 Jan 2014 05:53
by foxidrive
This should work:
Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage | find "LoadPercentage" >nul && test.exe
Re: Get CPU and Run a EXE
Posted: 25 Jan 2014 05:05
by shashank.kaneria
foxidrive wrote:This should work:
Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage | find "LoadPercentage" >nul && test.exe
AWESOME !!!
its working.
But in my scenario there is little bit more addition to be done, it should work only once whenever it find CPU more than 90%, it will run and then stop.
i am not able to handle if else condition so that i cant check if it worked once (means flag is 1) then do EXIT; like this
Yesterday I have tried d whole day to manage this small code to do this but all gone in vain.

Re: Get CPU and Run a EXE
Posted: 25 Jan 2014 07:12
by foxidrive
The test.exe will run and the batch file will quit, with the addition below.
Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage | find "LoadPercentage" >nul && (test.exe & goto :EOF)
Re: Get CPU and Run a EXE
Posted: 26 Jan 2014 23:30
by shashank.kaneria
foxidrive wrote:The test.exe will run and the batch file will quit, with the addition below.
Code: Select all
wmic cpu where "loadpercentage > 90" get loadpercentage | find "LoadPercentage" >nul && (test.exe & goto :EOF)
AWESOME !!!
Worked well. I was using some variables, Flags, if-else... hahaha
I have just made some baby like changes in this code (just an infinite loop)
Code: Select all
:BEGIN
wmic cpu where "loadpercentage > 1" get loadpercentage | find "LoadPercentage" >nul && (test.exe & goto :EOF)
GOTO BEGIN
Thank you so much... learnt a lot
Regards,
Shashank
Re: Get CPU and Run a EXE
Posted: 27 Jan 2014 09:40
by foxidrive
Glad it helped.
Your code will make constant checks and consume a fair bit of CPU by itself.
If you add a line after the WMIC line that delays a little bit then it will take far less CPU, such as this:
Code: Select all
ping -n 4 localhost >nul
or if you have Vista or later
timeout /t 3 /nobreak >nul
and it will delay for around 3 seconds between each check for CPU usage.
Re: Get CPU and Run a EXE
Posted: 28 Jan 2014 02:32
by shashank.kaneria
foxidrive wrote:If you add a line after the WMIC line that delays a little bit then it will take far less CPU, such as this:
Code: Select all
ping -n 4 localhost >nul
or if you have Vista or later
timeout /t 3 /nobreak >nul
Okay, I have added the same.
its working more smooth now.
GREAT.. !!!Regards,
Shashank