{"id":68903,"date":"2005-09-22T18:55:00","date_gmt":"2005-09-22T18:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/22\/how-can-i-monitor-the-activity-level-of-a-process\/"},"modified":"2005-09-22T18:55:00","modified_gmt":"2005-09-22T18:55:00","slug":"how-can-i-monitor-the-activity-level-of-a-process","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-monitor-the-activity-level-of-a-process\/","title":{"rendered":"How Can I Monitor the Activity Level of a Process?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I monitor the activity of a process to see if anyone is using it?<BR><BR>&#8212; AJ<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, AJ. You know, this is one time when writing the script is actually the <I>easy<\/I> part of the answer. Although there are a couple different ways to do this, we opted to periodically check the cumulative amount of processor time used by an application. If the amount of processor time never changes, that likely means that no one is using the application.<\/P>\n<P>But if that\u2019s the easy part of the answer then what\u2019s the hard part? Well, in this case the hard part involves deciding what counts (or doesn\u2019t count) as \u201cnot being used.\u201d In our sample script, we\u2019re going to measure the cumulative processor use 11 times, pausing 30 seconds between each measurement. When the script finishes we\u2019ll have a set of measurements spanning 5 minutes; our assumption is that if the processor time hasn\u2019t changed in the last 5 minutes then that means no one is using the application. That works fine for our sample script, but might not work as well in real life. After all, suppose your user is talking on the phone, taking a coffee break, or sitting in a meeting; in that case, it wouldn\u2019t be too surprising that an application would sit for 5 minutes (or more) without being used and without using up additional processor time. You\u2019ll need to decide for yourself what\u2019s reasonable for your purposes.<\/P>\n<P>We should also add that there\u2019s no reason why we have to take measurements every 30 seconds; we did that primarily so you could view the cumulative processor time on screen, and so that you\u2019d know for sure that the script was still working. We could just as easily take one measurement, wait five minutes, and then take a second measurement. Five minutes might not seem that long, but it\u2019s practically an eternity when you\u2019re waiting to see whether a script is actually doing anything or not. Again, you\u2019ll need to decide what\u2019s reasonable and what isn\u2019t.<\/P>\n<P>Yes, we know: that\u2019s all well and good, Scripting Guys, but where <I>is<\/I> this script you keep talking about? Well, right here:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>For i = 1 to 11<\/p>\n<p>    Set colProcesses = objWMIService.ExecQuery _\n        (&#8220;Select * from Win32_Process Where Name = &#8216;Notepad.exe'&#8221;)<\/p>\n<p>    For Each objProcess in colProcesses\n        sngProcessTime = (CSng(objProcess.KernelModeTime) + _\n                CSng(objProcess.UserModeTime)) \/ 10000000\n        Wscript.Echo objProcess.Name, sngProcessTime\n    Next<\/p>\n<p>    Wscript.Sleep 30000\nNext\n<\/PRE>\n<TABLE id=\"EED\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. This particular script monitors the process Notepad.exe, and assumes that there\u2019s only one instance of Notepad running on your computer; if there are (or could be) multiple instances of Notepad then you should monitor by process ID rather than process name.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The script begins by connecting to the WMI service on the local computer. We then set up a For Next loop to run 11 times, one for each measurement we plan to take. Inside the loop we use this line of code to return a collection of all the processes named Notepad.exe that are running on the computer:<\/P><PRE class=\"codeSample\">Set colProcesses = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Process Where Name = &#8216;Notepad.exe'&#8221;)\n<\/PRE>\n<TABLE id=\"ERD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Important<\/B>. Make sure this line of code is included <I>inside<\/I> your For Next loop; that\u2019s the only way to ensure that up-to-date process information is retrieved each time your run through the loop.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As it turns out, WMI tracks processor time using two different properties: KernelModeTime and UserModeTime. To track total processor time we need to add the values of these two properties. In addition, WMI tracks processor time in 100-nanosecond units. Because most of us don\u2019t think in terms of 100-nanosecond units (hey, we did say <I>most<\/I> of us) we also divide the sum of these two properties by 10,000,000, something that will convert 100-nanosecond units to seconds. Sure, it <I>sounds<\/I> complicated, but it takes only one line of code:<\/P><PRE class=\"codeSample\">sngProcessTime = (CSng(objProcess.KernelModeTime) + _\n    CSng(objProcess.UserModeTime)) \/ 10000000\n<\/PRE>\n<P>If you\u2019d rather calculate processor time in minutes, then divide the sum by 600,000,000 instead (10000000 x 60 seconds per minute).<\/P>\n<P>We echo the process name and total processor time, and then use the <B>Sleep<\/B> method to pause the script for 30 seconds (30,000 milliseconds). When 30 seconds are up, we loop around and repeat the whole process. After 11 such loops, the script terminates.<\/P>\n<P>When we run the script we get back information similar to this:<\/P><PRE class=\"codeSample\">notepad.exe 0.1602304\nnotepad.exe 0.801152\nnotepad.exe 0.801152\nnotepad.exe 0.8111664\nnotepad.exe 0.8111664\nnotepad.exe 0.8111664\nnotepad.exe 0.8111664\nnotepad.exe 0.8111664\nnotepad.exe 0.8111664\nnotepad.exe 0.8111664\nnotepad.exe 0.8111664\n<\/PRE>\n<P>As you can see, there was a very brief \u201cflurry\u201d of activity when we first began monitoring Notepad; for the last 8 measurements, however, Notepad hasn\u2019t used any processor time at all. (And, overall, the application has still used less than a second of processor time.) Does that mean that no one is using Notepad? Well, like we said, that\u2019s up to you to decide. After all, we work for Microsoft, and no Microsoft employee would <I>ever<\/I> presume to tell you what to do. (Well, pretty much.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I monitor the activity of a process to see if anyone is using it?&#8212; AJ Hey, AJ. You know, this is one time when writing the script is actually the easy part of the answer. Although there are a couple different ways to do this, we opted to periodically check [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,87,3,4,5,6],"class_list":["post-68903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I monitor the activity of a process to see if anyone is using it?&#8212; AJ Hey, AJ. You know, this is one time when writing the script is actually the easy part of the answer. Although there are a couple different ways to do this, we opted to periodically check [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68903\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}