HOWTO_Shutdown_headless_server_when_power-button_pressed
Contents |
Shutdown a headless server when power button pressed
To cleanly shut down a server when the power button is pressed, you will need:
- hardware that supports the ACPI power management features
- a kernel compiled with ACPI support
- the power button registered as an ACPI event
- acpid listening for ACPI events
- a handler for the power button event
Hardware support
Reboot your server and go into the BIOS. Look for a "Power Management" menu item/section. Under that, look for ACPI configuration settings. If you have ACPI configuration settings, then your BIOS supports ACPI. If you don't you'll have to stop here.
Kernel with ACPI support
As of the 2006.1 LiveCD, ACPI support is a configuration option during installation. If you engaged it, and you're using the gentoo-vanilla sources, then ACPI was compiled with your kernel. To check,
| Code: Using dmesg to check for kernel ACPI support |
dmesg | grep '^ACPI:' |
If you get any output, you have ACPI compiled in. If not, keep reading and we'll discuss getting it compiled in the next section.
Registering the power button as an ACPI event
In the kernel configuration file, ACPI power management section, there are several events you can register under ACPI: button, graphics, etc. The one we're concerned about is button, which registers button press events with the ACPI system. To see if you have the necessary support, return to dmesg:
| Code: Using dmesg to check for kernel ACPI support |
dmesg | grep '^ACPI: Power Button' |
If you get output, then your power button is registered. If not, then you'll need to either a) recompile the kernel with button registration engaged, or b) load the button module. In most cases, button registration will be available as a module, so let's give that a try:
| Code: Checking to see if button registration is a kernel module |
modprobe button lsmod | grep button |
If you receive no error from modprobe, and lsmod returns information, you've got button support. If either the modprobe or the lsmod failed, you'll need to recompile your kernel.
To summarize: you need the power button registered as an ACPI event. That's either built into your kernel, or is a kernel module. If it's a kernel module, you'll want to make it available at every reboot:
| Code: Ensuring power button registers in ACPI after reboot |
echo button >> /etc/modules.autoload.d/kernel-`uname -r | cut -f 1-2 -d .` |
Hello acpid? Listening for power button events
The ACPI daemon (acpid) listens for ACPI events and takes action when events are received. First, you need the acpid software running and available at boot:
| Code: Installing acpid |
emerge -av acpid /etc/init.d/acpid start rc-update add acpid default |
If you answered yes when asked to emerge acpid, and all went well, then you should be able to:
| Code: Ensuring acpid is available |
/etc/init.d/acpid status rc-update show | grep acpid |
and get positive confirmation that yes, acpid is running and yes, acpid will start in the default runlevel.
A handler for power button event
Following steps not required You're now ready to tell your server what to do when you press the power button. First, tell acpid what to do when the power button event is pressed:
| Code: Creating config file for acpid to catch power button press events |
vi /etc/acpi/events/power |
| File: /etc/acpi/events/power |
event=button/power.* action=/etc/acpi/onButtonPress-Power.sh |
Now, fill in /etc/acpi/onButtonPress-Power.sh with whatever you wish to do when the power button is pressed:
| Code: Example handler for power button press |
cat > /etc/acpi/onButtonPress-Power.sh <<EOT #!/bin/sh # /etc/acpi/onButtonPress-Power.sh # Shutdown when the power button is pressed logger "Power button pressed -- shutting system down" /sbin/shutdown -h now EOT chmod 750 /etc/acpi/onButtonPress-Power.sh |
Restart acpid (to load the changes you made in /etc/acpi/events/default).
| Code: Restart acpid |
/etc/init.d/acpid restart |
Testing
First, open up the relevant log files
| Code: Testing by reviewing logs while pressing power button |
tail -f /var/log/acpid /var/log/messages |
Now, press the power button. If you see activity, and the actions in your /etc/acpi/onButtonPress-Power.sh begin to take place, then you're all done.
Otherwise, go back and check your steps. Also, scour the logs above to see if anything obvious is out of place. Make sure that you have your /etc/acpi/onButtonPress-Power.sh executable -- this is a common mistake.
Created by NickStallman.net, Luxury Homes Australia
Real estate agents should be using interactive floor plans and real estate agent tools.
