Monitoring the activity of an running application or utility ...


I wrote a background script that monitors the activity of certain processes or applications. After a certain period of inactivity and when specific applications or processes are not running, the script returns to the login window and puts the computer to sleep.

At first, I used this little handler to monitor the activity of a process:

on GetProcessPercentCPU(someProcess)
	do shell script "/bin/ps -xco %cpu,command  | /usr/bin/awk '/" & someProcess & "$/ {print $1}'"
end GetProcessPercentCPU

But soon I realized that an application can be stalled, slowed or halted, but still running. So getting the % of use of the CPU is not the best way to monitor the process’s activity. I wrote a more complete handler that checked if the process was executing over a certain period of time. Here is the handler definition:

AppMaxActivityTest(theApp, consecutiveTrueTimes, timesDelayInSeconds, maxTestLenghtInMinutes, maxCPUHighLimit)

When this handler runs it returns a «out of delay» error message to the main script. How can I prevent that ?

Thanks in advance.


you can also retrieve the state of a process

do shell script "/bin/ps -xco state,%cpu,comm..

See man ps for all options of the state parameter

Hope it helps