Turning your Debian System off and back on

Something as everyday as turning your computer off and on seems like it should be an easy task, but we've advanced beyond flipping the power switch or being present to turn our system on, and sometimes the advanced features can be tricky to set up.

Hardware Power Control

You get your computer in a state where it's ready to lose power. The system would often give a reassuring statement like "Power off" or "Halted" or "It is now safe to turn off your computer." Then you toggled the power switch to off.

Smiple. :-)

Software Power Control

Then the computer industry decided to invent some new technology and spit out a couple new buzz words, ATX and APM.

ATX form factor

Now our computers (well, the BIOS and main board bits) start trying to decide how to manage the power states of our systems including powering off. The two state power switch is replaced overnight with a contact button that tells the BIOS "I was just pressed" and it's up to the BIOS to decide what that means. Generally it means "turn on" if it was off, and "turn off" if it was on, but what if you accidently bumped it, and how do you say "go to sleep" (new option with APM). Also, what should it do if the power goes out and comes back on later? The switch we flipped can no longer be 'flipped' with no power.

Also, UPS' gain little serial ports that tell the PC "power went out, switching to battery power". But what do we do when the UPS says that? Hope it's just a transient fault, and mains power will come back in a minute or two? Or do an orderly shutdown? What should we do when mains power comes back on later?

No longer as simple.

APM - Advanced Power Management

So, the BIOS configuration allows you to specify what parts of APM you want it to support and what to do in case there is a power outage. Some of the options include:

Another perk of APM is that it allows other software to interact with the power states of the main board. With it, a program on your computer can 'push the power button.'

For people using the Linux kernel it seemed the focus was on using APM only if you had a laptop on which you wanted to conserve battery power. This was especially true if you wondered if you should run apmd. See the ampd man page or http://www.worldvisions.ca/~apenwarr/apmd/

For the power off from software, the halt program had a -p option to tell the system to poweroff. The poweroff command is usually a symlink to halt that implicity acts like you called halt with the -p option. You do not need apmd to use poweroff. You may need apm support in the kernel (apm=on||apm=off).

ACPI - Advanced Configuration and Power Interface

APM was well and good, but it didn't give us all the options we wanted, like suspend to ram, suspend to disk, and that BIOS was in charge, not our software. ACPI turned this around putting the BIOS back in its place (low level communications, not giving orders). Unfortunately, now you have three ways for turning off your computer and your main board (and chipset); cpu, bios, and software all need to be on the same sheet of music to work it out right.

Since ACPI is supposed to replace APM, it seems that it should be out with the old and in with the new. Especially on SMP systems where APM may have caused race conditions. The answer seems to be 'maybe', or 'try this', or 'this or this or this and tell me what works'. Hopefully we can put together a good step by step APM or APIC OffAndOnAgain checklist so that you can get your computer to turn off like you'd like it to.

Off

Shutdown in a sentence

You give a command that tells init to switch to run level 0 for halt/poweroff or run level 6 for reboot.

Easy huh?

Shutdown explained

When init enters a run level, it kills some processes and runs some (other?) processes (man init). rc6.d happens to have an alias to /etc/init.d/reboot and rc0.d has an alias to /etc/init.d/halt. The reboot script simply calls /sbin/reboot and the halt script checks to see if halt means halt or if halt means poweroff, then calls /sbin/halt with the appropriate arguments. It also has a hook for apcupsd's ups-monitor script.

While you could use the telinit command to change to these run levels, you should use shutdown instead, since it's designed to "bring the system down gracefully", including telling logged in users that the system is shutting down and blocking new logins.

Many people use poweroff or reboot as aliases to the shutdown command since they can type less. Technically they are aliases to the halt command which sees that you're not at run level 0 or 6 and calls shutdown anyway.

To shut down now and halt but not poweroff:

 shutdown -hH now

To shut down and poweroff now:

 shutdown -hP now

To reboot now:

 shutdown -r now

To go to single user mode now:

 shutdown now

If you don't pass the H or P flag to shutdown -h (like typing poweroff doesn't), Debian looks at the HALT setting of /etc/default/halt to determine if it should power off or not. This allows you more flexibility if your BIOS doesn't give you the right 'do X after power is restored' option.

If you don't pass the -h option and try shutdown -P, it will drop you into single user mode because -h or -r weren't specified. The -P and -H options go with -h.

On

Pushing the power button to turn the computer back on is simple enough, even with APM or ACPI. For more advanced options if your BIOS and hardware support it, you could try WakeOnLan or other hardware wake events like your keyboard or mouse.

If you don't poweroff, but just halt your system (or if your system won't poweroff due to APM or ACPI problems), is there a way to remotely restart the system?

Observations and Checklist

Old Award BIOS v4.51 06/09/1998, Debian 3.1/Sarge Kernel 2.6.8

shutdown -hP now does not power off the machine. Tried kernel boot options apm=yes acpi=no with no luck. /proc/apm doesn't exist. Loading apm (modprobe apm) and then shutting down does power the system off. Kernel boot options for apm or acpi do not affect this one way or the other. Adding apm to /etc/modules made the fix persistant.

 dmesg
 ...
 ACPI disabled because your bios is from 98 and too old
 ...
 <nothing about apm until I loaded the module>

With the correct modules in modules.conf, the kernel tries ACPI, then falls back and attempts APM if ACPI isn't supported. You may never see anything out of your {APM,ACPI} infrastructure until you install acpid (or such).

There are numerous utilities out there for interacting with {APM,ACPI}, one of the best being Gkrellm, a Swiss Army Knife multi-tool mini-app which includes a battery power monitor builtin.

The Final Word

Of course, the question remains, what the heck are you doing turning off a Linux box anyway?!? :-) Sorry. Just a little joke.