Intuitively Obvious to the Most Casual Observer

Battery mode with custom systemd targets

Systemd’s targets are essentially glorified runlevels: they’re the chosen mechanism for easily grouping various services together, and succinctly describing the mode of operation of a system. Like all systemd units, they can have also dependencies, making management a bit easier. By creating a special “battery mode” target, we can facilitate easy or even automatic shutdown of unwanted processes when AC power is lost.

The default systemd setup has two targets of interest to us: and The latter depends on the former. On desktop and laptop systems, will be the default; on servers, only will be loaded. For laptop systems, we want to add two more: to replace in battery mode, and to do the same for

Creating these new targets is just a matter of copying the appropriate file from /usr/lib/systemd/system to /etc/systemd/system and deleting or changing the obvious lines. The resulting is:

Conflicts=rescue.service rescue.service

Now set up the dependencies you want by creating a directory and creating the appropriate symlinks, and the battery target is available for use.

Systemd’s equivalent to changing runlevels is isolate, which shuts down all processes not needed for the specified unit, and then starts that unit. You can restore a system to a “fresh” boot state (provided all processes have been started by systemd services) with systemctl isolate The last line enables running isolate on (Make sure to have the same line in if you’re using that.) Now you can switch into battery mode with

# systemctl isolate

and back out by isolating These commands can be put in the relevant /etc/acpi scripts to have them be run automatically, provided you’re using acpid.