The status of a process ...


I have been trying this one for a while but never got really somewhere. Maybe it is impossible. So that is why I submit this headache to the forum …

I want to monitor the activity (executing or idle) of an application or of a process running under «root». If a process is executing I want to use this condition as a criteria in a script. To find if an app/process «exists or not» is not sufficient. An app/process can be open but inactive.

Up to know I wrote a handler for application/process running under any user. It repeats a loop «x» times and gets the average of the CPU usage of that process. Depending on the CPU usage limit I choose, the result is going to determine if the app is «active» or inactive. It is not perfect but it is the best I could find up to now: This is the handler for processes running under any user:

--  Verify if the CPU usage of a process is over a specified limit (the handler returns the «activityStatus» of the process
on AppActivityStatus(theApp, testNumber, delayInSeconds, maxUsageOfCPU)
	tell application "System Events" to exists process theApp
	if result then 
		repeat testNumber times	
			set readingSum to readingSum + (GetProcessPercentCPU(theApp) as integer)
			delay delayInSeconds
		end repeat
		set readingAverage to ((readingSum / testNumber) as integer)
		if readingAverage < maxUsageOfCPU then
			set activityStatus to "inactive"
			set activityStatus to "active"
		end if
		set activityStatus to "closed"
	end if
	set readingAverage to 0
	set readingSum to 0
	return activityStatus
end AppActivityStatus

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

Is there a way to get the same status for those applications or processes running under «root» ?



Try substituting “A” for “x” in your ps options. It does the same as “x”, but also causes the processes belonging to other users (processes with UID different from the user running ps) to be included in the output. ps -Aco %cpu,command

Your technique of sampling over time and analyzing the data seems like a good approach to me. But, instead of using “%cpu”, you might try checking how much “cputime” changes over a period of time. You would have to decode the “minutes:seconds.fractions” output format.

The problem with “%cpu” is that it reflects only the percentage of CPU time used in the instant that ps was run. “cputime” will give you to the total amount of CPU time used since the process started, so you can control the sampling period.

If you sample “cputime”, you can see how much CPU time was used relative to the amount of “wall clock” time your script chooses. The farther such a ratio ((“cputime now”-“cputime then”)/“wall clock time”) is from 0 the more accurately “not idle” would characterize the process. This computed ratio is effectively the same as “%cpu”, but includes the processes activity over your entire sampling period instead of just at the two instants that ps was run. Of course this method would have problems for processes whose entire lifetimes were shorter than a decent multiple of your sampling interval.

One other thing is that CPU time is not the only possible measure of activity. An “I/O bound” process might use very little CPU time but be very busy accessing the disks or the network or talking to other processes. But even such processes will register some CPU time usage. It may just be a matter of tuning your sensitivity.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 4 Public Beta (4528.17)
Operating System: Mac OS X (10.4)