Energy Saver scripting

energysaverHere’s a version of a script we use on all our machines in an attempt to reduce energy usage with a minimum of visible impact on users. Our machines are set to not sleep during the day. This script runs hourly, and if it’s after 7pm and the machine has been idle for 20 minutes or more, it tries to sleep the machine if someone is logged in, or shut it down if no-one is logged in.
The machine is also set to automatically startup or wake at 6am M-F. The net result is that most of our desktop machines go to sleep or shutdown a little after 7pm each weeknight and wake up at 6am each week morning, and our users are none the wiser.


# Sleep or shutdown script
# tryin' to be 'green'.....

# look for exception file
if [ -f "/var/db/.dontSleep" ]; then
	exit 0

# if we're a laptop, exit.  
# No shutting down laptops (or waking them up unbidden!)
IS_LAPTOP=`/usr/sbin/system_profiler SPHardwareDataType | grep "Model" | grep "Book"`
if [ "$IS_LAPTOP" != "" ]; then
	exit 0

# check the time; exit if it's between 5 am and 7 pm
current_hour=`/bin/date +%H`
if [ $current_hour -gt 5 -a $current_hour -lt 19 ]; then
	exit 0

# now check idle time; 
# exit if we've been idle less than 20 minutes
idleTime=`ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=int((pop @F)/1000000000); print $idle,"\n"; last}'`
if [ $idleTime -lt 1200 ]; then
	exit 0

# tell Power Manager to wake us up or turn us on at 6am M-F
pmset repeat wakeorpoweron MTWRF 06:00:00

# check to see if a user's logged into the console
login_status=`/usr/bin/who | /usr/bin/awk '{ print $2 }'`
for i in $login_status; do
  if [ $i = "console" ]; then
       # someone's logged in, sleep
       osascript -e 'tell application "System Events" to sleep'
       exit 0

# if we got this far, it's OK to shut down.
/sbin/shutdown -h now
exit 0

Energy Saver scripting

13 thoughts on “Energy Saver scripting

  1. Brett_X says:

    Very nice.
    One related item comes to mind when reading your script: Is there any way to use ioreg to figure out what keeps my MacPro (at home) from going to sleep? Sometimes it does, sometimes it doesn’t.

  2. Grant says:

    BRILLIANT script! I’m going to implement this on a few test boxes and then to all my users right away.

  3. Andrew says:

    Brett_x – sometimes if you have an external device that draws power plugged into your MacPro constantly it will keep it from sleeping. I had a Sunto watch that charged and synced through USB. Even if the watch wasn’t connected the cable would wake the computer when I tried forcing it. Unplugged the cable and sleep functions returned.

    Greg, thanks for the script – I admin a University Production Center with film majors having 24/7 access to edit suites so something like this works perfect for me with high traffic and low traffic hours. Thanks!

  4. Neither: this actually runs as part of a meta StartupItem.

    I have a master startup item that simply runs all the scripts in a given directory; if I want to add a new script to run at startup, I just add it to the directory.

    This solution has worked for me since 10.2 – I probably won’t move it to launchd control until Apple forces me to, but in that case, it will still be a single script launched by launchd at startup that in turn runs all the scripts in a given directory.

    1. Troy Banther says:

      I’m a little new to scripting on OS X. Can you point me to a good source on how to work with and start using this type of script? You placed this in a folder and then had launchd run it at boot?

      1. Troy Banther says:

        LOL. It didn’t take me long to figure out where to place the scripts. Actually makes sense.

  5. irvesco says:

    Hey guy’s I know that I am late to the party, but I copied the script and pasted it to the apple script compiler. When I tried to compile the script, I got a Syntax Error that read Expected “,” or “]” but found “””.
    Could this be a Snow Leopard effect?

    1. No, the main problem is that the posted script is not an AppleScript! It’s a shell script, so you’d use a text editor that preserves UNIX line-endings and mark it as executable, and run it from the command-line.


  6. Mark says:

    Ok I’ve REALLY joined the party late. I ran across this script and must admit like what it does.

    However, I’m some what new to this stuff and I’m curious were do I place this script so that it will run every hour?

    I’m familiar with cron jobs but not launchd jobs of which it sounds like your not using?

    Thanks for the help

  7. Nico says:

    Yeah i need some help too. i know how to get this to be a login script but that doesnt help me. How can i get this script to run every hour on 10.6?



Comments are closed.