Author: Kern Sibbald

# Shutdown Sequence

If you experienced so problems with the testing procedures, or if you are porting apcupsd to another system, or you are simply curious, you may want to know exactly what is going on during the shutdown process.

The shutdown sequence is as follows:

- Apcupsd detects that there is a power problem and it calls /etc/apcupsd/apccontrol powerout, which normally sends a message to all users informing them of a potential problem.

- After approximately 5 seconds in the power problem mode, Apcupsd calls /etc/apcupsd/apccontrol onbattery, which normally sends a message to all users informing them that the UPS is on batteries.

- When one of the conditions listed below occurs, apcupsd issues a shutdown command by calling /etc/apcupsd/apccontrol doshutdown, which should perform a shutdown of your system using the system shutdown command. You can modify the behavior by editing the /etc/apcupsd/apccontrol script, but doing so will make it more complicated to upgrade to the next apcupsd version.

  The conditions that trigger the shutdown can be: running time on batteries have expired (TIMEOUT), the battery runtime remaining is below the configured value (BATTERYLEVEL), the estimated remaining runtime is below the configured value (MINUTES), or the UPS signals that the batteries are exhausted.

  A shutdown could also be initiated if apcupsd detects that the batteries are no longer functioning correctly. This case, though very unusual, can happen at any time even if there is proper mains voltage, and /etc/apcupsd/apccontrol emergency is called.

  Just before initiating any shutdown through the apccontrol script, apcupsd will create the file /etc/apcupsd/powerfail. This file will be used later in the shutdown sequence to recall apcupsd after syncing of the disks to initiate a power off of the UPS.

  If the /etc/nologin file has not already been created, it will normally be created during the shutdown sequence to prevent additional users from logging in (see the NOLOGIN configuration directive).

  Even though apcupsd has requested the system to perform a shutdown, it continues running. If it is a master with slaves, it will inform the slaves to do a shutdown. They perform their shutdown by calling /etc/apcupsd/apccontrol remotedown.

- When the system signals apcupsd to do exit, it does so. This is part of the normal system shutdown and the exact time that apcupsd receives the termination signal depends on how the shutdown links (usually in /etc/rc.d) are set.

- During the shutdown of the system after apcupsd has been forced to exit, one of the last things done by the system shutdown is to call the halt script, which is usually in /etc/rc.d/halt or /etc/rc.d/init.d/halt, or possibly in /sbin/init.d/rc.0 depending on your system. If apcupsd was properly installed, this standard halt script was modified to include a bit of new logic just before the final halt of the system. It first tests if the file /etc/apcupsd/powerfail exists, and if it does, it executes /etc/apcupsd/apccontrol killpower. It is this last step that will cause apcupsd to be re-executed with the --killpower option on the command line. This option tells apcupsd to inform the UPS to kill the power.

  This final step is important if you want to ensure that your system will automatically reboot when the power comes back on. The actual code used on the RedHat version is:

      # See if this is a powerfail situation.                               # ***apcupsd***
      if [ -f /etc/apcupsd/powerfail ]; then                                # ***apcupsd***
         echo                                                               # ***apcupsd***
         echo "APCUPSD will now power off the UPS"                          # ***apcupsd***
         echo                                                               # ***apcupsd***
         /etc/apcupsd/apccontrol killpower                                  # ***apcupsd***
         echo                                                               # ***apcupsd***
         echo "Please ensure that the UPS has powered off before rebooting" # ***apcupsd***
         echo "Otherwise, the UPS may cut the power during the reboot!!!"   # ***apcupsd***
         echo                                                               # ***apcupsd***
      fi                                                                    # ***apcupsd***

Please note that the above code should be inserted as late as possible in the halt script. On many systems, such as RedHat, all the disk drives were unmounted, then remounted read-only, thus permitting access to the /etc files and the apcupsd executable. If your system does not explicitly remount the disks, you must remount them in read-only mode in the code that you add. Examples of code fragments that do this can be found in the distributions/suse subdirectory of the source.

If you are not able to insert the above code in your halt script because there is no halt script, or because your halt script calls the init program as some Unix systems do, you can either just forget about powering off the UPS, which means that your machine will not automatically reboot after a power failure, or there is yet another alternative, though not at all as satisfying as inserting code in the halt script.

Only if you cannot insert the appropriate code in the halt script, when you start apcupsd, normally from the /etc/rc.d/init.d/apcupsd script, use the --kill-on-powerfail option. This will cause apcupsd to program the UPS to shutoff the power just before it (apcupsd) does the system shutdown. Please note that this is not the most ideal solution. Read on to understand why.

A very important consideration is that you must set the EEPROM in your UPS so that it waits a sufficient time for the system to halt before it shuts off the UPS power. The current value as well as the permitted values for your UPS can be determined by executing:

apcaccess eeprom

The output should look something like the following:

    apcaccess eeprom

    Valid EPROM values for the SMART-UPS 1000

                             Config        Current  Permitted
    Description              Directive     Value    Values
    ===================================================================
    Upper transfer voltage   HITRANSFER    253      253 264 271 280
    Lower transfer voltage   LOTRANSFER    196      196 188 208 204
    Return threshold         RETURNCHARGE  0        00 15 50 90
    Output voltage on batts  OUTPUTVOLTS   230      230 240 220 225
    Sensitivity              SENSITIVITY   H        H M L L
    Low battery warning      LOWBATT       2        02 05 07 10
    Shutdown grace delay     SLEEP         20       020 180 300 600
    Alarm delay              BEEPSTATE     0        0 T L N
    Wakeup delay             WAKEUP        0        000 060 180 300
    Self test interval       SELFTEST      336      336 168 ON  OFF

The line of interest for you is the Shutdown grace delay, which can be changed using the SLEEP directive in your apcupsd.conf file. The default value is 20 seconds, but generally, you can set it to 180, 300, or 600 seconds depending on your UPS. See the EEPROM programming section of this manual for further details on how to change this EPROM value. --kill-on-powerfail option, you run the risk of having the computer power cut before the system has shutdown. Even if the grace period is rather long, if something goes wrong in the shutdown, well, it is up to you to decide.

## Automatic Reboot of your Computer after a Power Shutdown

If apcupsd has successfully shutdown your computer and powered off the UPS during a power outage, you can control whether or not your computer is automatically rebooted when the power returns.

The UPS contains two internal EPROM values that determine when it will restore power to your computer after a full power shutdown. They are the RETURNCHARGE percentage and the WAKEUP delay. Briefly, the RETURNCHARGE specifies what percentage charge the battery must have before the power is restored. Higher values are recommended in regions where the power goes up and down frequently. The WAKEUP delay is a simple time delay. Most sites will have both of these at zero, or perhaps the RETURNCHARGE set to 15. Please follow the links to the Configuration section of this manual for more information. See the EEPROM programming section of this manual for further details on how to change these EPROM values.

The final consideration for a automatic reboot after a full power down is to ensure that your computer will automatically reboot when the power is restored. This is not the normal behavior of most computers as shipped from the factory. Normally after the power is cut and restored, you must explicitly press a button for the power to actually be turned on. You can test your computer by powering it down; shutting off the power (pull the plug); then plugging the cord back in. If your computer immediately starts up, good. There is nothing more to do. If your computer does not start up, manually turn on the power (by pressing the power on button) and enter your computer's SETUP program (often by pressing DEL during the power up sequence; sometimes by pressing F10). You must then find and change the appropriate configuration parameter to permit instant power on. Normally, this is located under the BOOT menu item, and will be called something such as Restore on AC/Power Loss or Full-On. The exact words will vary according to the ROM BIOS provider. Generally you will have three options: Last State, Power On, and Power Off. Although Last State should normally work, I set my computers to Power On. This means that whenever the power is applied they are on. The only way to shut them off is to pull the plug or to have a special program that powers them off (/sbin/poweroff on Linux systems).

## Shutdown Problems

Obviously if your halt script is not properly modified, apcupsd will not be able to shut off the power to the UPS, and if the power returns before the batteries are exhausted your system will not automatically reboot. In any case, your machine should have been cleanly shutdown.

## Master/Slave Shutdown

In master/slave configurations, however, the master cannot be 100 percent sure that the slaves have all shutdown before it performs the power off. As a consequence, it is possible that the master will shut off the power before the slave has finished shutdown. If this is the case, the best procedure is to put an appropriate sleep command in the /etc/apcupsd/apccontrol file on the master. For example to give the slaves 30 additional seconds to shutdown, one would add:

sleep 30

just after the line that reads

doshutdown)

in the apccontrol file (approximately line 79 - depending on your system version).

Also, on a slave machine, you do not want to use the modified halt script since it will recall apcupsd, which will detect that it is a slave (i.e. no connection to the UPS) and will complain that it cannot do the killpower. This situation is not harmful just annoying and possibly confusing.

One possible problem during shutdown can be caused by remnants of old versions. Please be sure to delete or rename all prior versions (/usr/local/sbin/apcupsd or /sbin/powersc).

## Startup

Normally, apcupsd is automatically started when your system is rebooted. This normally occurs because the startup script apcupsd is linked into the appropriate places in /etc/rc.d. On most Linux systems, there is a program called chkconfig that will automatically link the startup script. This program is invoked by the make install scripts, or it is explicitly done for those systems that do not have chkconfig. If this is not the case, you can either link it in appropriately yourself or explicitly call it from your rc.local file. The appropriate manual way to startup apcupsd is by executing:

<path>/apcupsd start

where <path> is normally /etc/rc.d or /etc/rc.d/init.d depending on your system (isn't Unix wonderful? :-)). Using this script is important so that any files remaining around after a power failure are removed. Likewise, shutting down apcupsd should be done with the same script:

<path>/apcupsd stop