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,