UPS shutdown for Xserves

JF Mezei wrote on :

When one plugs in a UPS (via USB) to an Xserve with OSX (Snow Leopard), the "Energy Saver" system preference is augmented to include UPS features that include when a shutdown should be made etc.

How would I go about finding documentation on exactly what OS-X does with thsoe signals from the UPS ? (for instance, is there a bash (or other) script that is called during such a shutdown that could take various actions that I would want taken ?

More importantly, if there is a UPS initiated shutdown, how doe OS-X react when power is reapplied ?

The UPS documentation recommends that the UPS perform the power off before a shutdown is done so that the server knows it is recovering from a power failure when power is restored (and automatically boots) whereas if you do a proper shutdown, when the power is re-applied, the server may not boot automatically.

I'd like to see Apple's version of how a UPS is to behave in these circumstances.

David Empson replied on :

JF Mezei jfmezei.spamnot@redacted.invalid wrote:

When one plugs in a UPS (via USB) to an Xserve with OSX (Snow Leopard), the "Energy Saver" system preference is augmented to include UPS features that include when a shutdown should be made etc.

That feature applies to all desktop Mac models running Mac OS X, not just the Xserve (and not just Mac OS X Server). The Xserve may have some additional options due to its more advanced power management.

I have a UPS on my Mac Mini Server (running Server 10.6.8), configured to shut down after 40 minutes of running from UPS power.

How would I go about finding documentation on exactly what OS-X does with thsoe signals from the UPS ? (for instance, is there a bash (or other) script that is called during such a shutdown that could take various actions that I would want taken ?

Energy Saver is just providing a graphical user interface to the power management subsystem, which is controllable with more detail from Terminal using the 'pmset' utility.

man pmset

pmset has a variety of configuration options for various power states (battery, mains, UPS), and when to shut down in reaction to UPS power.

In my case, I had to use pmset to set my desired options, because Energy Saver didn't allow configuration of a delay longer than 15 minutes. (It appears my UPS is not fully compatible with the driver in Mac OS X 10.6, because the computer can sense whether it is running on UPS power, and reports the model in System Preferences, but always shows the UPS battery level as 100%. Mine is an APC Smart-UPS 750, model SMT750I.)

From reading the man page, the UPS-triggered shutdown is classifed as an "emergency shutdown", but it isn't entirely clear what this means.

A little Internet research reveals that the script at /usr/libexec/upsshutdown is invoked in order to do the emergency shutdown.

This invokes "shutdown -hu now", which in turn will invoke "halt -u".

The man page for halt has more information (see the -u option, which I've quoted further down).

Halting will do an orderly termination of running processes via SIGTERM then SIGKILL, then flush the cache and turn off power (-u varies the handling at the point it would normally power down).

I'm not familiar enough with the shutdown logic of Mac OS X to know whether the "Shut Down" invoked from the Apple menu (and elsewhere in the GUI) is a higher level mechanism, but I expect it is, and halt is normally invoked as the last step after all GUI applications are shut down. A UPS-triggered emergency shutdown is therefore likely to be more abrupt than an orderly shut down from the GUI, with GUI applications being terminated quickly (effectively everything will get a Force Quit).

I can't see mention of any mechanism to invoke a command on changes of power state, or when an emergency shutdown is triggered. One solution would be to modify /usr/libexec/upsshutdown, but it is a standard system component so is at risk of being restored whenever you install an update.

What you could do is have a script running which wakes up periodically and checks the current power state via pmset -g and parsing the output to find the appropriate line. It can then react to switching to UPS power.

Once it knows that UPS power is active, it could wake up in response to the SIGTERM from halt and quickly perform essential tasks, but won't have long before it gets forcibly killed via SIGKILL.

More importantly, if there is a UPS initiated shutdown, how doe OS-X react when power is reapplied ?

man halt and see the -u option.

"The system is halted up until the point of removing system power, but waits before removing power for 5 minutes so that an external UPS (uninterruptible power supply) can forcibly remove power. This simulates a dirty shutdown to permit a later automatic power on. OS X uses this mode automatically with supported UPSs in emergency shutdowns."

The UPS documentation recommends that the UPS perform the power off before a shutdown is done so that the server knows it is recovering from a power failure when power is restored (and automatically boots) whereas if you do a proper shutdown, when the power is re-applied, the server may not boot automatically.

This is what is supposed to happen (from a post on Macintouch, copied from an older Apple Discussions post):

<http://www.macintouch.com/readerreports/batterybackup/topic3535.html#d2 9apr2009>

  1. AC Power lost
  2. UPS switches to battery power
  3. Computer tell UPS to turn off the outlets in five minutes.
  4. Computer saves/closes all work, starts shutting the OS down.
  5. Computer stalls the shut down process once all critical files and drives are secured.
  6. Computer enters a hung state and waits for the remainder of the five minutes.
  7. The UPS turns off the outlets.
  8. The computer, which is still on, dies due to a power failure with no risk to data or drives thanks to its semi-shutdown state.
  9. When the electricity comes back on, the UPS turns the outlets back on. 10 The computer, having failed due to a power loss, is able to start back up on its own.

What is not clear is the case where the UPS doesn't turn off power within five minutes (so the computer turns itself off) but mains is restored before the UPS turns off its outputs. In this case, the computer has no way to know that the computer needs to start up again (the SMC can't talk to the UPS, since it has no access to USB or serial ports).

As long as the UPS is compatible with the Mac OS X drivers, it should correctly implement the delayed power off within the five minute window.

Jolly Roger replied on :

In article 4edf3974$0$8493$c3e8da3$cc4fe22d@redacted.invalid, JF Mezei jfmezei.spamnot@redacted.invalid wrote:

When one plugs in a UPS (via USB) to an Xserve with OSX (Snow Leopard), the "Energy Saver" system preference is augmented to include UPS features that include when a shutdown should be made etc.

How would I go about finding documentation on exactly what OS-X does with thsoe signals from the UPS ? (for instance, is there a bash (or other) script that is called during such a shutdown that could take various actions that I would want taken ?

More importantly, if there is a UPS initiated shutdown, how doe OS-X react when power is reapplied ?

The UPS documentation recommends that the UPS perform the power off before a shutdown is done so that the server knows it is recovering from a power failure when power is restored (and automatically boots) whereas if you do a proper shutdown, when the power is re-applied, the server may not boot automatically.

I'd like to see Apple's version of how a UPS is to behave in these circumstances.

If you want full control over such things, use APC UPS Daemon instead:

http://www.apcupsd.org

JF Mezei replied on :

David Empson wrote:

Energy Saver is just providing a graphical user interface to the power management subsystem, which is controllable with more detail from Terminal using the 'pmset' utility.

Many thanks for your explanations.

But I think I have a problem then. My goal is to have the Xserve shutdown, but modem/router and VoIP ATA continue to run on UPS (at which point, they would have an extended run time since they draw little power).

I'll have to investigate the various options in the pmset and shutdown and halt commands.