Hi,
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"
else
set activityStatus to "active"
end if
else
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» ?
Regards.
Robert