I'm hoping you can help me with this, I've tried researching for hours but can't seem to get anywhere with it.
I have a process which randomly starts to consume 50+ CPU a few times a day, I'm trying to create a batch file which will run in the background and restart the service if the CPU usage of the process gets above 50 (fixes the issue).
I found something very close on this forum, a guy named "Elektro Hacker" answered, but his answer is the overall CPU usage, not just the usage of a specific process. Here is the batch file he suggested:
@Echo OFF
SET "SERVICE=Themes"
SET /A "MAXUSAGE=95"
SET /A "INTERVAL=5"
:LOOP
For /F %%P in ('wmic cpu get loadpercentage ^| FINDSTR "[0-9]"') do (
IF %%P GTR %MAXUSAGE% (
Echo [%TIME:~0,8%] CPU Usage: %%P%% Reached the limit: %MAXUSAGE%%%
Echo Restarting %SERVICE% ...
SC STOP "%SERVICE%" 1>NUL
SC START "%SERVICE%" 1>NUL
Echo Service restarted.
) ELSE (
Echo [%TIME:~0,8%] CPU Usage: %%P%%
)
)
Ping -n %INTERVAL% Localhost >NUL
GOTO :LOOP
He also put: "For more precission maybe you want to check the current CPU percentage of the executable associated to the service using:
wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessor
Time
This is the part I can't get past, I keep getting errors, and every time I try to fix it by changing it slightly, I get different errors!
Can anybody change this code slightly, to get it to get the CPU of say "MyProcess" The process and service names are different, I'm not sure if this would make a difference but thought it was worth mentioning.
My coding knowledge is minimal at best, so any help or advice you could give me is greatly appreciated, this is driving my crazy!
Thanks a lot!
Powesshell can do better
Remove the space:
You could also do:
You need to use
^
to escape the comma from Name,PercentProcessorTime. It should look like:But this is just correct in terms of syntax - findstr will match "ProcessName %Load", whereas the script wants to match only "%Load".
Unfortunately, I'm not proficient in Windows batch scripts and am neither looking for that, so I had to use a third party software called GAWK (gnu awk) to be able to output only the "%Load" part.
This is the script I am using:
NB1: SERVICE=ProcessName - without .exe!
NB2: I'm appending .exe with: "%SERVICE%".exe
NB3: I'm using taskkill instead of sc stop/start