기본적으로 올바르지 만 조각이 올바르게 작동하는지 확인하기 전에 먼저 뛰어 오르는 것처럼 보입니다. 또한 처리기와 변수의 이름을 지정하려고하면 도움이 될 수 있습니다. 예를 들어,이 경우 처리기가 앱을 모니터링하고 낮은 CPU 사용량에 도달하면 해당 앱을 종료하는 것처럼 보입니다.
예에서 프로세스 이름을 TaskPaper
으로 변경했는데 사용할 수 있으므로 유의하십시오. 쉘 스크립트는 우리가 원하는 수를 반환하는 것을, 그리고 문자열로 반환 있다고 :이 시점에서
quitOnLowCPU("TaskPaper")
on quitOnLowCPU(processToMonitor)
set processCPU to do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processToMonitor & "$/ {print $1}'"
display dialog processCPU
end quitOnLowCPU
, 우리는 두 가지를 알고있다.
숫자를 안정적으로 비교하려면 숫자 값으로 변환해야합니다.
quitOnLowCPU("TaskPaper")
on quitOnLowCPU(processToMonitor)
set processCPU to do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processToMonitor & "$/ {print $1}'"
--convert the shell script response string to a number
set processCPU to processCPU as number
--compare to the threshold of quitting
if processCPU is less than 2.0 then
tell application processToMonitor to quit
end if
end quitOnLowCPU
이 작동하지만, 그것은 또한 실행되지 processToMonitor
도 processToMonitor
경우 종료하려고합니다.
quitOnLowCPU("TaskPaper")
on quitOnLowCPU(processToMonitor)
set processCPU to do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processToMonitor & "$/ {print $1}'"
if processCPU is "" then
--the process is gone. We're done
return
end if
--convert the shell script response string to a number
set processCPU to processCPU as number
--compare to the threshold of quitting
if processCPU is less than 2.0 then
tell application processToMonitor to quit
end if
end quitOnLowCPU
이제 우리는 핸들러 주위에
repeat
를 추가 할 준비가 : 끝없이 반복 스크립트는 종종 자신의 CPU 돼지가 될 수 있기 때문에
quitOnLowCPU("TaskPaper")
on quitOnLowCPU(processToMonitor)
repeat
set processCPU to do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processToMonitor & "$/ {print $1}'"
if processCPU is "" then
--the process is gone. We're done
return
end if
--convert the shell script response string to a number
set processCPU to processCPU as number
--compare to the threshold of quitting
if processCPU is less than 2.0 then
tell application processToMonitor to quit
end if
delay 1
end repeat
end quitOnLowCPU
내가 각 반복에 delay
을 추가했다.