LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Laptop and Netbook (https://www.linuxquestions.org/questions/linux-laptop-and-netbook-25/)
-   -   Boot & Shutdown Notebook on Power Connection & Loss-is this possible? (Xubuntu) (https://www.linuxquestions.org/questions/linux-laptop-and-netbook-25/boot-and-shutdown-notebook-on-power-connection-and-loss-is-this-possible-xubuntu-909691/)

silntdoogood 10-23-2011 02:22 PM

Boot & Shutdown Notebook on Power Connection & Loss-is this possible? (Xubuntu)
 
Long version:
I have xubuntu installed to a laptop. This system needs to shut down when AC power is disconnected.

I don't know of a setting manager that is able to configure this. If there is one, I'd love to know of it. Ideally, the system should shut down a minute or two after power is lost. I am also trying to get the laptop to boot when it is plugged in. Booting isn't AS much of a problem, because it can be solved with a little WOL, but it would be a much smoother operation if it could bring it's self up too. I can't use WOL to shut the box down because the router will have lost power at the same time as the system.

Short version:
How can I set up a distro installed to a laptop to boot and shut down when power is connected, and disconnected, respectively.

Thanks
-Spence

Nominal Animal 10-23-2011 10:42 PM

You need BIOS/hardware support to automatically boot whenever AC power is connected. The OS, be it Linux or whatever else, cannot help you with that. So, check your BIOS or equivalent. If your laptop BIOS or equivalent does not provide that option, hardware modification is possible, but will void any warranties, so I would not recommend it unless you are a DIY person.

The timed poweroff after loss of AC power should be easy. Since you use Xubuntu, and therefore XFCE, you should already have xfce4-power-manager in your panel; if not, install it. Right-click on the icon, and select Preferences. In the Battery section, select 'Shutdown' for 'When battery power is critical'. In the Extended section, select say 90% for 'Consider the computer on low power at'. Since AC power equates to about 100%, your laptop should now automatically power down after a short while. This obviously works only if you are logged on, and only for you personally.
_________________________________________________

In general, most Linux laptops run acpid, a daemon which provides ACPI events (like AC connect and disconnect, lid events) to userspace programs. Its configuration varies a bit between Linux distributions, but in general, it is configured via a set of simple shell scripts under /etc/acpi/. If you cannot or do not want to rely on the xfce4-power-manager stuff, for example because you want the laptop to power off even from e.g. the login screen, you can use the acpid interface instead.

First, you need a small shell script to do the timed shutdown. We'll later on tell acpid to run it whenever AC power is lost. The script must wait for the specified duration, and keep checking if AC power is regained. If AC power is regained during this interval, the script must of course exit (and not shutdown the laptop). Short AC losses are surprisingly common. So, assume you save this as /usr/local/bin/ac-loss-shutdown.bash:
Code:

#!/bin/bash

# Read the configuration from /etc/ac-loss.conf:
#    TIMEOUT=300
#    INTERVAL=5
#    DIRNAME=/tmp/ac-loss/
# TIMEOUT is the AC loss time in seconds before shutdown,
# INTERVAL is the interval in seconds between AC power checks,
# and DIRNAME is the location of the flag directory.
# Defaults are shown above, if /etc/ac-loss.conf does not exist.

[ -f /etc/ac-loss.conf ] && . /etc/ac-loss.conf

TIMEOUT=$[ $TIMEOUT -0 ]
INTERVAL=$[ $INTERVAL -0 ]
[ $TIMEOUT -gt 0 ]  || TIMEOUT=300
[ $INTERVAL -gt 0 ] || INTERVAL=5
[ -n "$DIRNAME" ]  || DIRNAME="/tmp/ac-loss/"

# If "$DIRNAME" exists, this script is already running.
mkdir -m 0600 "$DIRNAME" &>/dev/null || exit 0

# When we exit, we want the flag directory to be removed automatically.
trap "rm -rf '$DIRNAME'" EXIT

# Unix timestamp when timeout expires (UTC, no Daylight Savings mess)
EXPIRES=$(date -u +%s -d "now + $TIMEOUT seconds")

# Wait until timeout expires.
while [ $(date -u +%s) -lt $EXPIRES ]; do

    # If any of the AC adapters is on-line, we have AC power.
    grep -qe on-line /proc/acpi/ac_adapter/*/status &>/dev/null && exit 0

    # Otherwise, sleep for a few seconds.
    sleep "$INTERVAL"

done

# Time to shutdown!
/sbin/poweroff

In Xubuntu, the 'AC lost' action is defined in /etc/acpi/events/battery. Normally, it points to /etc/acpi/power.sh. However, /etc/acpi/power.sh is used for other events too, so we'll need to modify it to point to (nonexistent, yet) /etc/acpi/battery.sh instead:
Code:

# /etc/acpi/events/battery
# Called when AC power goes away and we switch to battery

event=battery
action=/etc/acpi/battery.sh

Finally, create /etc/acpi/battery.sh to both do the original action, and to run our custom /usr/local/bin/ac-loss-shutdown.bash too:
Code:

#!/bin/bash
[ -x /usr/local/bin/ac-loss-shutdown.bash ] && ( /usr/local/bin/ac-loss-shutdown.bash &>/dev/null & )
exec /etc/acpi/power.sh "$@"

These modifications to acpid configuration keep your normal ACPI actions intact, while still adding the extra script. You don't want to lose e.g. powersaving features by skipping the ac/battery events.

I hope you find this instructive,

silntdoogood 10-26-2011 06:21 PM

Quote:

Originally Posted by Nominal Animal (Post 4506190)
...hardware modification is possible, but will void any warranties, so I would not recommend it unless you are a DIY person...

It's an older laptop, the and no longer is under warranty. I'm decent with hardware modifications. The original plan was going a persistent install on a flash-drive. This is how I came to running Xubuntu. It was a slow and faulty filesystem. While digging through my box of dead drives, I found enough parts to make one more functioning one. XD I think I am going to just install an extension cord off the existing power button. It's just annoying tiny SMT connections, and if there was an easy software solution, I wanted to know.

Quote:

Originally Posted by Nominal Animal (Post 4506190)
...The timed poweroff after loss of AC power should be easy. Since you use Xubuntu, and therefore XFCE, you should already have xfce4-power-manager in your panel; if not, install it...

I don't think the utility was installed initially. I'm running an older distro but, I will admit, I might not have looked in the right place. I found and ran the install for http://goodies.xfce.org/projects/app...-power-manager and it appeared in nearly every menu I opened. Thanks for all of the help! This was exactly what I needed!

Edit:
Upon further inspection, I can not set the "critical level" above 20%. Any time I try to set it 21% or more, it goes to the default 20%.


All times are GMT -5. The time now is 09:43 AM.