# APC UPS - PC won't power on after AC outage

## Robert S

I recently purchased an APC UPS (UPSPRO 550VA/330W).  I've installed apcupsd and set the appropriate DEVICE parameter in /etc/apcupsd/apcupsd.conf.  I have not changed any other settings.  I'm using gentoo 64 bit with the latest "stable" updates.

If I power off the UPS at the wall, it appropriately shuts down my computer after about 30min, but when I restore power to the UPS, my computer does not power on.  I have set the BIOS so that the computer will power on after a loss of power, and this has worked when I'm not using the UPS.

I assume that the problem is that my computer has done a "graceful" shutdown, and the power-on is not triggering a restart.  I need to do this manually by pressing the soft-on button on my computer.  Looking at some comments on the net, this is the desired behaviour, because another power failure can occur and result in data corruption.

Because my computer is a "headless" machine, it is often not possible to switch the power on manually. As I keep regular backups I could accept the small risk of data loss in the event of multiple power failures.

Is it possible to change the shutdown scripts so that my computer will recognise that AC power has been lost, and power on again?  What is the "safest" way of doing this?  I assume that one of the commands in the config directory can be changed.  Alternatively, can I change settings in apcupsd.conf to allow my desired behaviour?

Here is my /etc/apcupsd/apcupsd.conf file: *Quote:*   

> UPSCABLE usb
> 
> UPSTYPE usb
> 
> DEVICE /dev/usb/hiddev0
> ...

 

----------

## Fitzcarraldo

Have you seen the instructions for doing this in the APCUPSD User Manual?: http://apcupsd.org/manual/manual.html#arranging-for-reboot-on-power-up

----------

## eccerr0r

I've not tried using apcupsd, but perhaps this will work:

I don't know if all UPS support this, but my old BestPower does support "delayed shutdown" - basically you can tell the UPS to shut off power a set number of minutes from the command.  You issue that command to the UPS, and then do a normal shutdown - but without powerdown.  The computer will "hang" after shutdown and the UPS will cut power to the computer.  The computer will now remember its state as "powered up."

Then the computer should come back up when power is restored.

Will this work, or is this what you've already tried?

----------

## Robert S

In response to both replies: I have already set the BIOS to "Power On".  Maybe I should try "Last state".

I'm looking at the option of changing the shutdown script in /etc/apcupsd

The apcupsd manual suggests examining the halt script - there isn't one in gentoo (it is a binary) - Can someone can advise what the equivalent is?

Most of the time the default is probably best - having to manually turn the machine on after a prolonged outage would guarantee that there's no loss of data, and we get warning about most prolonged outages.  I would however like to know how to change this.

I don't think that I can set a delay into the UPS

----------

## cboldt

I think the equivalent under openrc is /etc/init.d/bootmisc, which calls `halt -w`

The manpages for halt and shutdown might help too.

I'm somewhat interested in the resolution to your problem, but for a different reason at the moment.  I have one machine that is not on UPS, and it goes down hard on power outage.  That machine responds to wakeonlan, but only "if conditions are right," which I think is related to how it was shutdown - if I do a `shutdown -h now`, no wakeonlan.  I do not want it to restart automatically on restoration of power, but I would like for it to restart if power is on and it gets a wakeonlan.  It is normally on 24/7, so I do not want to set BIOS to "last state" or "on."

----------

## eccerr0r

I think that these are different issues, might be better to use different topics.

For the OP, I doubt that keeping BIOS at former-status will help.  Worth a shot? why not.

After the computer shuts down, does the UPS stay up or does it shut down with the computer?  You might need to connect a lamp or something to see if it's still powering the outputs.

How does the UPS wake up the PC normally?  Just the power status?  Some can wake through USB if it's connected that way, and you'll need to set up wake on USB.

----------

## cboldt

From the apcupsd page linked, the PC is expected to restart itself on power restore, with no additional input.

"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."

From the remarks on that page, I take it that a `shutdown -h now` -- or any similar command -- results in LOSS of the necessary start on restoration of power.

----------

## Fitzcarraldo

 *man shutdown wrote:*   

> 
> 
> -h
> 
>     Requests that the system be either halted or powered off after it has been brought down, with the choice as to which left up to the system.
> ...

 

Try the following from a console to check if the OS shuts down (halts) but the PC stays powered up:

```
shutdown -H now
```

If that works, you could alter the power-loss script for apcupsd so that it issues that command when the UPS tells apcupsd to shutdown. The OS would then shutdown correctly but the PC would remain powered up until the UPS has drained, at which point the PC would of course power-off. Startup of the PC on resumption of power supply from the UPS would still be contingent on the PC's BIOS having been configured so that it starts automatically upon supply of power:

 *APCUPSD User Manual wrote:*   

> 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, we recommend setting your computers to Power On. This means that whenever the power is applied they are on.

 

P.S. I also have a headless server which uses apcupsd, and I also have an APC UPS connected to it (an APC Back-UPS ES 700 in my case). I chose not to configure the server BIOS and apcupsd to boot automatically upon resumption of power supply to the server. If I happen to be at home, I would press the Power On button on the server; if I happen to be away on a work trip (the most likely scenario), I would telephone home and ask someone to press the Power On button. This has only happened once in my case.

----------

## Robert S

I've had a chance to look at this.

If I unplug my PC and plug it in again after a "graceful" shutdown, it powers on automatically.  The BIOS is therefore doing the right thing.

If I power off the UPS at the wall and wait for it to halt my PC, the monitor remains on - therefore the UPS is still supplying battery power to my monitor and PC.

```
# /sbin/apcupsd --killpower
```

causes the UPS to power down after a delay. I had AC power on, so the PC  powered off then on again - presumably as expected.

I therefore conclude that my shutdown scripts aren't causing my UPS to stop supplying power to my PC.  Therefore it won't boot up when the power comes back on again.

I have added /etc/init.d/apcupsd.powerfail to my shutdown runlevel - as recommended.  Looking at the code this should cause the UPS to switch power off after a delay, but it doesn't seem to be doing this.  Here is my shutdown runlevel: *Quote:*   

>  # rc-update show |grep shutdown
> 
>     apcupsd.powerfail |                        shutdown
> 
>             killprocs |                        shutdown
> ...

 

I therefore conclude that my UPS is continuing to supply battery power after my PC shuts down.  Can anyone tell me how to remedy this?

----------

## Fitzcarraldo

An excerpt from /etc/apcupsd.conf on my server is as follows:

 *Quote:*   

> # PWRFAILDIR <path to powerfail directory>
> 
> #   Directory in which to write the powerfail flag file. This file
> 
> #   is created when apcupsd initiates a system shutdown and is
> ...

 

and my server has a script named killpower in $PWRFAILDIR, which I modified for my purposes, having left the commented-out lines in the original script installed by apcupsd:

```
#!/bin/sh

#

# This shell script if placed in /etc/apcupsd

# will be called by /etc/apcupsd/apccontrol before

# apcupsd kills the power in the UPS. You probably

# need to edit this to mount read-only /usr and /var,

# otherwise apcupsd will not run.

# Choose one of this solution

#mount -n -o ro /usr

#mount -n -o ro /var

#

#mount | awk '/ext2/ { print $3 }' | while read line; do

#       mount -n -o ro,remount $line

#done

#mount | awk '/ext3/ { print $3 }' | while read line; do

#       mount -n -o ro,remount $line

#done

#mount | awk '/reiserfs/ { print $3 }' | while read line; do

#       mount -n -o ro,remount $line

#done

#exit 0

# 2016-06-25: Fitzcarraldo commented out everything above and added the lines below:

cat /home/fitzcarraldo/apcups/ups-email-killpower.txt | /usr/sbin/sendmail -4 -t

sudo -u fitzcarraldo ssh vmachine1 sudo shutdown -h now

sudo -u fitzcarraldo ssh vmachine2 sudo shutdown -h now

sleep 30

shutdown -h now

exit 0
```

To achieve what you want, my guess is that you need to modify $PWRFAILDIR/killpower as per the commented-out lines quoted above, viz.:

 *Quote:*   

> # This shell script if placed in /etc/apcupsd
> 
> # will be called by /etc/apcupsd/apccontrol before
> 
> # apcupsd kills the power in the UPS. You probably
> ...

 

----------

## Fitzcarraldo

Further to my previous post, I also noticed the following in the APCUPSD User Manual:

http://apcupsd.org/manual/manual.html#verifying-a-source-installation

 *Quote:*   

> Modification of the halt script is important so that at the end of the shutdown procedure, apcupsd will be called again to command the UPS to turn off the power. This should only be done in a power failure situation as indicated by the presence of the /etc/powerfail file, and is necessary if you want your machine to automatically be restarted when the power returns. On a Red Hat system, the lines containing the # ***apcupsd*** should be inserted just before the final halt command:
> 
> ```
> # Remount read only anything that's left mounted.
> 
> ...

 

Looking at the Gentoo powerfail init script:

```
#!/sbin/openrc-run

# Copyright 2009 Gentoo Foundation

# Distributed under the terms of the GNU General Public License v2

# $Id$

description='Signal the UPS to kill power in a power failure condition'

depend() {

        need mount-ro

}

start() {

        if [ "$(runlevel | cut -d' ' -f2)" = "0" -a -f /etc/apcupsd/powerfail ] ; then

                ebegin 'Signaling UPS to kill power'

                /sbin/apcupsd --killpower

                eend $?

        fi

}
```

perhaps you need to modify it as per the APCUPSD User Manual so that it explicitly calls the killpower script mentioned in my previous post:

```
#!/sbin/openrc-run

# Copyright 2009 Gentoo Foundation

# Distributed under the terms of the GNU General Public License v2

# $Id$

description='Signal the UPS to kill power in a power failure condition'

depend() {

        need mount-ro

}

start() {

        if [ "$(runlevel | cut -d' ' -f2)" = "0" -a -f /etc/apcupsd/powerfail ] ; then

           echo

           echo "APCUPSD will now power off the UPS"

           echo

           /etc/apcupsd/apccontrol killpower

           echo

           echo "Please ensure that the UPS has powered off before rebooting"

           echo "Otherwise, the UPS may cut the power during the reboot!!!"

           echo

           eend $?

        fi

}
```

----------

## Fitzcarraldo

If the above does not work, another thing to consider is whether Gentoo is shutting down USB before it runs the killpower script. Apparently this is what happens in Ubuntu 16.04 specifically: APCUPSD does not send killpower command to UPS (only happens on Ubuntu 16.04):

 *JanCeuleers wrote:*   

> According to discussion on the apcupsd-users mailing list [1] this issue is OS-specific rather than being a problem in upstream apcupsd.
> 
> Specifically it is believed that this issue is due to Ubuntu shutting down USB before the killpower command is issued to the UPS, which which it is no longer possible to communicate without USB.
> 
> [1]: https://marc.info/?t=147606479000001&r=1&w=2

 

----------

## Robert S

I have discovered that the apcupsd.powerfail script has an error.  If I modify the script to display the runlevel, it displays it as "unknown", and therefore the "/sbin/apcupsd --killpower" command never runs.  I've modified the script as below:

```
#!/sbin/openrc-run

# Copyright 2009 Gentoo Foundation

# Distributed under the terms of the GNU General Public License v2

# $Id$

description='Signal the UPS to kill power in a power failure condition'

depend() {

        need mount-ro

}

start() {

        if [ "$(runlevel)" = "unknown" -a -f /etc/apcupsd/powerfail ] ; then

                ebegin 'Signaling UPS to kill power'

                /sbin/apcupsd --killpower

                eend $?

        fi

}

```

It all works as expected now.

Can anybody see any situation where this could cause problems through the reporting of $runlevel as "unknown"?

Otherwise I'll send in a bug report.

----------

## cboldt

That "unknown runlevel" is weird.  What do you get if you just run `runlevel` from the command line?

The issue isn't with the the script, per se.  It has to do with how openrc sees itself at that point in the reaction to the powerfailure.

----------

## Robert S

 *Quote:*   

>  ~ # runlevel
> 
> N 3

 

I get "unknown" when I start in recovery mode. As far as I can remember I have always had this behaviour.  I seem to remember that I've had this happen with every installation of gentoo that I've done.

Any suggestions as to how (or whether) I should deal with this?  I agree that it doesn't look right.

----------

## cboldt

I haven't studied the details (obviously), I don't use apcupsd at all, just following along here.  But, reading the APC material, much of it is tailored to RedHat scripts, and it may be that the Gentoo implementation is more DIY and less "emerge it and it works."

From your earlier ...

```
apcupsd.powerfail | shutdown

killprocs | shutdown

mount-ro | shutdown

savecache | shutdown 
```

Out of those four, the only script that even questions what runlevel it is in, is apcupsd.powerfail

Now, maybe apcupsd.powerfail has some generic design, where the script can be called other than from the openrc shutdown level, and knowing the runlevel matters.

But it seems to me that in the case outlined in this thread, looking for the file "/etc/apcupsd/powerfail" and checking runlevel is belt and suspenders.  Fitzcarraldo's killpower script is another case of highly customized code, compared with what is provided by the apcupsd package.

On startup, notice what gets rid of etc/apcupsd/powerfail ... `/etc/init.d/apcupsd start`  The mere presence of /etc/apcupsd/powerfail is enough to tell us the system is in a powerfail situation.

----------

## cboldt

Digging a little more, it seems the script in question is derived from /etc/init.d/powerfail

That script, at least the reference to it that I find in a goggle search, is a Debian construct called by /etc/inittab, not by openrc.

----------

## Fitzcarraldo

cboldt, indeed I had to alter significantly the scripts in /etc/apcupsd/, including the script /etc/apccontrol that calls them. In my case I did not want my server to tell the UPS to go into hibernation (which is what the command '/sbin/apcupsd --killpower' does) when apcupsd runs the killpower script, hence my modifications to /etc/apcups/killpower listed in a previous post. In my case, I edited /etc/apcupsd/killpower to do the same thing as /etc/apcupsd/doshutdown just to be sure, and I configured the BIOS not to boot automatically when the power supply is restored. My understanding of the intended process is as follows:

1. Mains supply ceases.

2. UPS tells apcupsd that the mains supply has ceased.

3. apcupsd uses $BATTERYLEVEL, $MINUTES, and $TIMEOUT (set in /etc/apcupsd.conf) to determine when to shutdown the OS (Step 4 below).

4. apcupsd runs /etc/apcupsd/doshutdown to initiate shutdown of the OS.

5. After the OS initiates shutdown, the OS uses apcupsd (which runs /etc/apcupsd/killpower) to tell the UPS to go into hibernation. The Gentoo apcupsd.powerfail init script only tells apcupsd to put the UPS into hibernation when the OS is in Runlevel 0 and the OS has almost completed shutting down (the file systems have already been mounted Read-Only).

6. I think the UPS goes into hibernation $KILLDELAY seconds after /etc/apcupsd/doshutdown runs (setting KILLDELAY=0 in /etc/apcupsd.conf stops apcupsd from running /etc/apcupsd/killpower if I understand correctly, and I have set KILLDELAY=0).

In my case I was not interested in making my UPS go into hibernation once the OS shuts down. To be honest, I did not want to risk the UPS going into hibernation before my server had shutdown the OS and powered down the server. Furthermore, it is not the only device powered by the UPS. Currently I have configured apcupsd in my server to initiate shutdown of the OS either when the remaining UPS battery power has dropped to 5% or there is only 3 minutes of runtime left (as calculated by the UPS). This is achieved by setting BATTERYLEVEL=5, MINUTES=3 and TIMEOUT=0 (zero means no check on how long the server has been running on battery power) in /etc/apcupsd/apcupsd.conf.

----------

## Akkara

 *Fitzcarraldo wrote:*   

> Currently I have configured apcupsd in my server to initiate shutdown of the OS either when the remaining UPS battery power has dropped to 5% or there is only 3 minutes of runtime left (as calculated by the UPS). This is achieved by setting BATTERYLEVEL=5, MINUTES=3 and TIMEOUT=0 (zero means no check on how long the server has been running on battery power) in /etc/apcupsd/apcupsd.conf.

 

Does your UPS have lead-acid batteries (sealed or otherwise)?  If so, you'll get much better life out of the batteries if you shut down nearer to 30%.  Most UPS batteries are undersized as it is, and will fail after only a year when run down this hard.

If you *have* to keep your server running because that's how you assure it powers back up, then set the UPS turn-off threshold to 11.25V (if it uses a 12V battery).

----------

## Fitzcarraldo

Thanks for the advice, Akkara. My APC Back-UPS ES 700 uses a 12V sealed lead-acid battery.

apcupsd does not appear to have a parameter to be able to configure a voltage threshold. The three parameters it uses to determine when to trigger shutdown are:

 *APCUPSD User Manual wrote:*   

> 
> 
> BATTERYLEVEL percent of battery
> 
>     If BATTERYLEVEL is specified, during a power failure, apcupsd will halt the system when the remaining battery charge falls below the specified percentage. The default is 5 percent. This directive is ignored for dumb (voltage-signalling) UPSes. To totally disable this counter, set BATTERYLEVEL -1 in your apcupsd.conf file.
> ...

 

Quote from the APCUPSD User Manual: http://www.apcupsd.com/manual/manual.html#configuration-directive-reference

Anyway, I'll take your advice and change BATTERYLEVEL to 30 (%) instead of the default 5. Actually, the powercuts in my location are few and far between (the frequency is once annually or even less), although we get a few micro power cuts lasting a second or two during a year, usually overnight (my guess is switchgear switching).

Coming back to what I wrote in previous posts regarding putting the UPS into hibernation ('killpower') at shutdown, I think my caution and my disabling of it are justified:

 *APCUPSD User Manual wrote:*   

> KILLDELAY time in seconds
> 
>     If KILLDELAY is set, apcupsd will continue running after a shutdown has been requested, and after the specified time in seconds, apcupsd will attempt to shut off the UPS the power. This directive should normally be disabled by setting the value to zero, but on some systems such as Win32 systems apcupsd cannot regain control after a shutdown to force the UPS to shut off the power. In this case, with proper consideration for the timing, the KILLDELAY directive can be useful. Please be aware, if you cause apcupsd to kill the power to your computer too early, the system and the disks may not have been properly prepared. In addition, apcupsd must continue running after the shutdown is requested, and on Unix systems, this is not normally the case as the system will terminate all processes during the shutdown.

 

----------

