Fix IT Scripts
1 like 0 dislike
How do you set up a script to monitor the % of Processor used by a particular process and create an alert if it meets or exceeds the threshold?

DOS, of course...
asked in Operating Systems by (887 points) | 43 views
I wanted this recently. We have 4 MSSQL instances running, and one runs away with the processor.. Now all instance processes are running the same named SQL executable. Monitoring an individual process that has duplicate names is a pain, and still not found an answer. Hopefully someone will be along soon!
Not a perfect script, but it takes the process name (sans the .exe) and the percentage you want to compare it against and either exits with an error or a pass. (result = 1001 or 0)
This may be what you are looking for:
Assisted Solutionby:LiberatingInsight
ID: 400130672014-04-21
I found a solution...
First I run task list /v /fo csv and output that to a file
Then I run the following:
typeperf "\Process(*)\ID Process" -sc 1 > %tempdir%\ProcessPerf.csv && typeperf "\Process(*)\% Processor Time" -si 10 -sc 6 >> %tempdir%\ProcessPerf.csv

The above gives me an output of the PID for all of the running process and the matching TyePerf name and outputs it to a CSV file.  Then the second part of the command adds 6 sample sets taken 10 seconds apart.

Now I can review the processes and if something is out of range I can track it back to the Process ID and user that has it running.

Thank you very much for your help.
I was playing around with this, though, and it has promise:
@echo off
set PROC=%1

for /f "tokens=2" %%a in ('tasklist^|find /i "%PROC%"') do (echo set PID=%%a)
for /f "tokens=2" %%a in ('tasklist^|find /i "%PROC%"') do (SET /A "CMDCNT+=1")


1 Answer

1 like 0 dislike


@echo off

REM Variables
SET Process=%1
SET /a "Limit=%2"

for /f "skip=2 tokens=2 delims=," %%c in ('typeperf "\Process(%Process%)\%% Processor Time"  -sc 1') do (
set /a "cpu_usage=%%~c"
goto :break

REM devide overall usage by number of cores or processors
set /a "cpu_usage=cpu_usage/%NUMBER_OF_PROCESSORS%"

if +%cpu_usage% LSS +%Limit% (
    goto :end
) else (
    goto :over

Echo "Process using %cpu_usage%, at or over threshold"
Exit 1001

Echo "Process using %cpu_usage%"
Exit 0

answered by (887 points)

Related questions

0 like 0 dislike
0 answers
0 like 0 dislike
1 answer
asked Aug 11, 2017 in Windows OS by doughall (3.1k points) | 32 views
0 like 0 dislike
1 answer
asked Aug 10, 2017 in Operating Systems by doughall (3.1k points) | 34 views
0 like 0 dislike
1 answer
30 questions
24 answers
634 users