Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Auto sleep based on CPU usage System
In this Intelligent sleep/energy saver? thread on the forum site, Novajo offers up a script that can be used to put your Mac to sleep based on CPU load. The script is quite short:
set theLoad to do shell script "uptime | sed 's/.* //g'"

if (theLoad as number < 0.8)
tell application "Finder"
sleep
end tell
end if
Save the script, make it executable, and then use 'cron' to schedule it to run at regular intervals, and your Mac will put itself to sleep when the CPU load has been below your threshhold.

The forum thread has more info on the cron task if you'd like to see how that step is done...

[Editor's note: Script changed to reflect comments posted below - thanks all!]
    •    
  • Currently 3.00 / 5
  You rated: 2 / 5 (4 votes cast)
 
[6,224 views]  

Auto sleep based on CPU usage | 7 comments | Create New Account
Click here to return to the 'Auto sleep based on CPU usage' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Doesn't work
Authored by: sjonke on Feb 12, '02 03:25:05PM

For me the do shell script command returns nothing, nor does using the command at the terminal. uptime itself works fine. Something wrong with the awk arguments?



[ Reply to This | # ]
Shell command
Authored by: robg on Feb 12, '02 03:48:03PM
What happens in the shell when you type:
uptime | awk '{ print $12 }'
On may machine, it returns the last uptime, which is the 15-minute average. I haven't tried the whole script, but the correct return of the substring indicates that it would..

[ Reply to This | # ]
Shell command
Authored by: sjonke on Feb 12, '02 04:45:38PM

It appears to return a blank line (rather, a line with nothing on it is displayed)



[ Reply to This | # ]
uptime...
Authored by: robg on Feb 12, '02 05:38:49PM
What does 'uptime' return? Here's the two commands on my machine:
% uptime
2:36PM up 4 days, 21:20, 1 user, load averages: 2.60, 1.81, 1.45
% uptime | awk '{ print $12 }'
1.39
It sounds like your uptime command is returning something other than the three-argument output. -rob.

[ Reply to This | # ]
uptime...
Authored by: jiclark on Feb 12, '02 07:39:56PM
rob, I get:
5:34PM up 4:35, 3 users, load averages: 0.00, 0.00, 0.00
when I do the "uptime" command, but nothing (just a blank line) when I do the other command. Is there something wrong here? How to fix?? Thanks, John P.S. Why the "3 users" item? It's just me on this machine... Weird.

[ Reply to This | # ]
uptime...
Authored by: clindberg on Feb 12, '02 08:05:36PM

Change the command to "uptime | sed 's/.* //g'"

That will pull off the last "word" of uptime's output, which
I think is what the author wanted. uptime's output does
not have a consistent number of whitespace-delimited "words",
i.e. sometimes it's "18:59" and sometimes it's "19 hours", so
using awk to get the 12th "word" doesn't always work.

Also, you get a different "login" every time you open a Terminal
window. It really is logging you on again... "3 users" is a
tiny bit misleading, it really means 3 login sessions.



[ Reply to This | # ]
uptime...
Authored by: ret on Feb 13, '02 08:02:34PM
If you know you want the last column of the output, you can use:
uptime | awk '{print $NF}'
As inferred elsewhere in this thread, $1...$12 refer to field 1... field 12. NF is a variable (presumably read-only) that refers to 'number of fields' in this line. $NF dereferences to value of last field. You can also do tricky stuff like $(NF-2) I don't know do much Applescript, but there is some sort of construct like
set theVar equal to the last word in theOutput
which would probably do the same thing if you preferred. Hope that helps, RET

[ Reply to This | # ]