Thursday, June 11, 2020

Terminal sleep for old Macs

Even though my daily driver for almost the last two years has been a POWER9 Raptor Talos II, prior to that I used a Power Mac Quad G5 for almost 13 years. Although that system is on its second liquid cooling assembly, it still works, and any machine with that long of a service record is going to have lots of files stored you need to periodically access and old applications you need to periodically run. (And I'm still building TenFourFox on it, too, so there's that.)

For many things I'm connected to it via VNC or even just switching the KVM to its console. However, the G5 is not a particularly power-thrifty machine and I'd rather not have two big beefy computers running at the same time under my desk when it's 100 F out in sunny Southern California, so I regularly have it in sleep. Since many of my other typical tasks can be completed from the Mac OS X command line, it would be nice if I could sleep and restart it from a remote terminal login as well.

As it turns out, at least for 10.4 and up, this is relatively simple to do. To sleep the G5, I have a very simple shell script that looks like this:

#!/bin/csh -f

say "switch kvm"
sleep 5
osascript -e 'tell application "Finder" to sleep'
sleep 10
date

The magic incantation here is the AppleScript (run by osascript), which tells the Finder to sleep the system, and acts just as if you had done this from the Apple menu even if you're not actually on the console. The rest of it is oddiments of my routine: the say reminds me to switch the KVM away if I run this script from a Terminal.app session if I am using the console, since activity on USB will cause the G5 to wake up, and the two sleeps give me time to switch if necessary and give the AppleScript enough time to run. The date command is there because initially I couldn't figure out why it kept waking up (turned out to be a defective PCIe card which I pulled) and I wanted to correlate the time it awakened with the system log, but it's just handy to know anyway.

So that's sleeping — what about waking it up? The G5 will respond to Wake-On-LAN packets in sleep mode, so it's just a matter of sending it one. There are a couple of different command-line implementations; I personally rather like this simple Java one (especially since it doesn't need privileges) or this slightly more complex Perl version.

You would think you would pass these scripts the IP of the system and its MAC address, and you would be wrong. You do indeed need to know the MAC address of the destination system which you can get from arp or ifconfig, or for Power Macs from Mac OS X by going to System Preferences, Network, selecting the Ethernet interface in question, and then clicking the Ethernet tab. However, the IP address you'll send it to is actually the broadcast address for the network. ifconfig can tell you this as well if you don't know it. For many people, with this example assuming you used the Java version with javac WakeOnLan.class, that command for a typical home 192.168.1.* network would look like

java WakeOnLan 192.168.1.255 00:01:02:03:04:05

where the xy:zg:hi:jk:lm:no digits at the end are the MAC address of the destination system. Once in a great while, because this is UDP, you will need to send the packet twice if the machine fails to acknowledge it. I put this into a separate shell script on my T2 so that I just run it to wake the G5 up, shell in, sleep it when I'm done and either leave the window open (for the next time I wake it up) or just cut the connection. Now the G5 is always ready when I need it and in the way I need to access it, and not driving up my electric bill when I don't.

No comments:

Post a Comment

Comments are subject to moderation. Be nice.