Search:  
Gentoo Wiki

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:

  1. hardware that supports the ACPI power management features
  2. a kernel compiled with ACPI support
  3. the power button registered as an ACPI event
  4. acpid listening for ACPI events
  5. 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.

Note: You do not need to have ACPI enabled in the BIOS for your server to handle ACPI events. I generally leave ACPI disabled in the BIOS and configure my power management features in the operating system -- this provides more control and can be ghosted for mass installations.


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 .`
  


Tip: Reboot your box now, then run through the dmesg checks (above). If it works, great, we're ready to move on. If not, go back and fix the mistakes.

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:

Note: This steps not required in latest versions of acpid, default scripts just works.
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.

Retrieved from "http://www.gentoo-wiki.info/HOWTO_Shutdown_headless_server_when_power-button_pressed"

Last modified: Fri, 05 Sep 2008 23:24:00 +0000 Hits: 13,881