Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Get an alert if a program uses to much CPU Time

Post image for Get an alert if a program uses to much CPU Time

by Steve Wiseman on September 30, 2011 · 6 comments

in Tips,Tools,Utility,Windows


.

Got a question from Brett this week:

“Hi Steve. I have a tough one for you. I want to determine from a VBScript if a program is using too much CPU time. Specifically, I want to know when the spooler service goes over 50% CPU utilization. All the scripts I see don’t seem to work like task manager (The CPU time reported by the script is much higher). Thanks, and keep it up.”

Thanks Brett. I did some testing, and indeed you are correct that all of the scripts I found report CPU usage differently than task manager.

CPU Usage in Task Manager

The reason for this is that the scripts are reporting the percentage of usage on one specific core.

What you need to do is find out how many cores there are and then divide the cpu usage by that number.

After lots of experimenting with different versions of Windows, I came up with a script that will do this on Windows XP through 2008 R2.

Click here to download the script
(Rename to .VBS after downloading)

If you open the script, and scroll to the bottom…you can see how to use it.

In your case, we want to check the spooler process. On Windows 7 it is named “spoolsv”. I want to check once a minute, and if it goes over 50%, the script will show a message box.

Here is how I would do that:

while (true)
 'Check our spooler for over 50% cpu time
 if (check_process_cpu_time("spoolsv",50)) then
  MsgBox "Spooler is running high on CPU Time!"
 end if
 'Wait a minute before checking again
 WScript.Sleep(60000)
wend

I wouldn’t set the checking time for less than a minute, since the script itself takes some work to get this info.

Also, notice that the name you use for the process does not include the file extension. In our example we want to watch spoolsv.exe, so we provide spoolsv to the script.

This is just a demo, but you cold have it send an email using blat or, have it send you an windows message using the msg command in windows – the possibilities are endless.

Now Brett will know when his printer spooler is jacked up, before he starts getting phone calls.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 6 comments… read them below or add one }

1 Tim C October 4, 2011 at 9:53 pm

Awesome tip steve. I have had a spooler problem for quite some time. It is a faulty driver. Got to use it, and just need to restart when it starts causing trouble.

The problem is, I am usually at lunch when the thing takes a dive. I put your little script into action, except I got it to restart the spooler for me. Amazing how few calls I get a lunch now for just this one issue.

Thanks a million!

2 Jennifer October 4, 2011 at 10:30 pm

Hi Steve. Jen here. Thanks for your help last time.

Does this script work on Windows 2000?

3 Steve Wiseman October 4, 2011 at 10:31 pm

No – unfortunately the script does require Windows XP or later. I believe the performance libraries are not available from VB Script in Windows 2000, and that is why it will not work.

Steve

4 Limbo February 26, 2012 at 11:51 am

Hi, very good script.
How can we know if any process is using more than 50%?

Thanks.

5 david March 29, 2012 at 12:00 pm

Quick question about modifying this. I need to get notified when a process goes down to 0.

I tried changing the value to 0 from 50 (and changed to the correct process) but it doesnt work. Is there a way to trigger the notification if it falls less than 1?

THanks in advance!

6 Ciprian December 19, 2012 at 10:54 pm

Hi, Steve!

Very useful script! The script works great when you have only one process instance running. When more are running the script looks after only the first one. Is there any way to take into account all of them?

Leave a Comment

Category Links - Windows Forum - Exchange Forum