Mac OS X Energy Scripting
We’ve got a variety of Macs running OS X versions 10.4, 10.5 and 10.6. They’re all managed from an Xserve with MCX preferences through Workgroup Manager, but for some reason I’ve just never managed to get them all to settle nicely with their power management settings.
While it’s lovely to have the control of these things through Workgroup Manager, I finally got miffed with it not working, particularly with the Tiger clients. It’s also not good to have rooms of machines burning away power and their components pointlessly.
‘Terminal’
So I did what I usually do. Write a script. There’s an excellent blog called Managing OS X which had already covered this issue, so using GregN’s script as inspiration I came up with this, in Perl for some reason.
#!/usr/bin/perl -w
## powerman.pl v1.07 (2nd September 2014) by Andy Davison
## Ensures basic power management settings are maintained and
## powers down machines when we think we can get away with it.
use strict;
my $clires;
my $logfile = '/var/log/powerman.log';
my $lockfile = '/tmp/powermanpmsetdone';
my $caffeine = '/tmp/stayawake';
# Log housekeeping (erase log if >100KB).
$clires = `touch $logfile`;
my $logsize = -s $logfile;
if ($logsize > 102400) {
$clires = `echo > $logfile`;
}
# Let us alter the power management settings if we force it.
if ("@ARGV" =~ 'force') {
$clires = `rm $lockfile 2>/dev/null`;
}
# Figure out where we are.
my $hostname = `hostname`;
# Set some basics, unless we've already done so recently.
if (! -e $lockfile) {
my $pmbasics = `pmset -a ttyskeepawake 1 hibernatemode 0 halfdim 1 womp 1 sleep 0 repeat wakeorpoweron MTWRF 08:45:00`;
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: UPDATE : General power management settings have been applied." >> $logfile`;
if ($hostname !~ 'cluster') {
my $pm = `pmset -a displaysleep 20 disksleep 0 autorestart 1`;
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: UPDATE : Studio-specific power management settings have been updated." >> $logfile`;
}
if ($hostname =~ 'cluster') {
my $pm = `pmset -a displaysleep 30 disksleep 20 autorestart 0`;
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: UPDATE : Cluster-specific power management machines have been updated." >> $logfile`;
}
}
# Now we get active with some potential shutdowns.
my $currenthour = `/bin/date +%H`; chomp $currenthour;
my $idletime;
my $consoleuser;
my $ttysuser;
my $sysload;
# How long have we been idle?
my @idle = `/usr/sbin/ioreg -c IOHIDSystem`;
foreach (@idle) {
if (/Idle/) {
my @idlevalue = split /= /, $_;
$idletime = int((pop @idlevalue)/1000000000);
last;
}
}
# Is anybody using the console?
my @console = `/usr/bin/who`;
foreach (@console) {
if (/console/) {
$consoleuser = 1;
}
if (/ttys/) {
$ttysuser = 1;
}
}
# Are we madly processing away on something?
my @uptime = `uptime`;
foreach (@uptime) {
my @load = split / /, $_;
$sysload = int((pop @load));
last;
}
# Things to do if we're outside our usual hours (0800-2100).
if (($currenthour < 8) || ($currenthour > 20)) {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: snore : powerman.pl starting an out-of-hours check..." >> $logfile`;
# If there's no console user, nobody on a terminal session, the system has been idle 20 mins and there's not much activity, shut down.
if (($idletime > 1200) && ($consoleuser < 1) && ($ttysuser < 1) && ($sysload <= 2)) {
if (! -e $caffeine) {
if ($hostname =~ "cluster") {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: SHUTDOWN : Shutting down the system due to lack of activity out-of-hours." >> $logfile`;
my $shutdown = `/sbin/shutdown -h now`;
} else {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: If this had been a cluster machine, it would have been shut down due to lack of out-of-hours activity." >> $logfile`;
}
} else {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: CAFFEINE : A process has requested we stay awake (with no console user) using $caffeine" >> $logfile`;
}
}
# If there IS a console user, but they've been inactive for an hour and there's barely a hint of processor usage, shut down.
if (($idletime > 3600) && ($consoleuser == 1) && ($ttysuser < 1) && ($sysload <= 1)) {
if (! -e $caffeine) {
if ($hostname =~ "cluster") {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: SHUTDOWN : Forcing an out-of-hours shutdown with an inactive console user present." >> $logfile`;
my $shutdown = `/sbin/shutdown -h now`;
} else {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: If this had been a cluster machine, we would have forced an out-of-hours shutdown with an inactive console user present." >> $logfile`;
}
} else {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: CAFFEINE : A process has requested we stay awake (console user present) using $caffeine" >> $logfile`;
}
}
} else {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: heartbeat : powerman.pl just completed an in-hours check." >> $logfile`;
}
if (! -e $lockfile) {
$clires = `echo "\`date +'%Y-%m-%d %H:%M:%S'\`: heartbeat : powerman.pl successfully completed it's first run for this boot." >> $logfile`;
$clires = `touch $lockfile`;
}
exit 0The idea is that this runs via launchd however often you would like. Using Lingon to create a .plist file, I have it triggered every hour. It uses the hostname of the machine to determine where it is, with different rules applied if the system is a ‘cluster’ or a ‘studio’ machine. Once per boot it reapplies the power management policy, and on every run it examines the state of the machine and decides whether it should be powered down.
Lifted straight from GregN’s script is the idle time discovery; this is perfect for testing whether someone is using a machine ‘out of hours’ and not shutting it down prematurely if they are. :)
One extra feature in this script is the processor usage check. As we occasionally farm out jobs via Xgrid or Logic Node, it’s not good to turn off while processing something. While I wouldn’t say that this bit has been extensively tested, it does seem to work pretty well for us at the moment.