Visualising your progress by having monitors running, displaying automated testing runs, is a valuable tool in software development.
However, one concern that arises when adopting this approach is the use of energy. Powered off monitors are cheaper to run than powered on monitors, there’s no argument there.
Showing this progress during the day though is valuable so we do it, but there is something we can do something to minimise our environmental impact, turning off those monitors automatically at the end of the day and switching them on automatically. Not only does this help save energy but it also is a prompt for developers to go home, a central part of sustainable pace of development.
So we have a mac that turns on at 8am and off at 8pm which has a selenium slave that runs our tests in firefox on it.
Problem is using the normal, mac sleep schedule crashes the running selenium slave. So every time we come in in the morning we have to restart the slave. Not great.
Being programmers we decided to fix this with code. The best approach seemed to be, to only switch the monitor off not sleep the whole system. Next problem is macosx doesn’t by default let you do this. So we had to solve it by creating a launchd deamon to run an ssh script on the mac at 8pm.
The bash script (screenSleep.sh) we stored in a local directory and looks like this:
WAKEUP=$(date -v+12H "+%m/%d%/%Y %H:%M:%S")
echo $WAKEUP > /tmp/screenSleepWakeupTime.txt
pmset schedule wake "$WAKEUP" > /tmp/screenSleepOutput.txt
Basically, it takes the current date, and adds twelve hours (8pm->8am). Then it runs the pmset command to schedule a wakeup. Then it waits 5 seconds to give the system a chance to catch up. Then it sets the display to sleep.
The launchD daemon looks like this and was stored in /Library/LaunchDaemons/de.razorfish.displaySleep.plist :
<?xml version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Important things to note:
<string>de.razorfish.displaySleep</string> has to be the same as the file name.
The script has to be saved by root as it will be run as root, only root can run these commands.
To load the launchD daemon you need to run:
"sudo launchctl load /Library/LaunchDaemons/de.razorfish.displaySleep.plist"
If you want to see if it loaded run :
"sudo launchctl list"
If you totally fuck it up don’t worry, run “sudo launchctl unload /Library/LaunchDaemons/de.razorfish.displaySleep.plist” and start again.