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:
say "switch kvm"
osascript -e 'tell application "Finder" to sleep'
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.