home |  electronics |  toolbox |  science club |  tuxtalk |  photos |  e-cards |  online-shop



The Linux server shutdown button

I run a little server at home. It runs the print server, a web server, a file server. It sits in the corner without keyboard or monitor and we shut it down unless we need it.

To make it easy for the whole family to safely shut it down this Linux server has a few "shutdown button" options:

Configure the power button to run shutdown + power off

If you install Ubuntu server and then pressing the power button while the server is running might already case the server to safely power down. Try to see what it does. It depends on whether or not you are running a desktop and other configuration options but it might work right out of the box.

If not the check the following things.

The power button generates an ACIP event and you need to have acpid installed and running to act on it (install acpid: apt-get -y install acpid). The acpid will run the script /etc/acpi/powerbtn.sh when the power button is pressed. Normally /etc/acpi/powerbtn.sh will just perform a few checks to see if it has to pass that button press information to somebody else. Most of the time that is systemd-logind.

You can however short-circuit the chain and simply add at the top of /etc/acpi/powerbtn.sh:
    /sbin/shutdown -h now "Power button pressed"
    exit 0


If systemd is running then /etc/acpi/powerbtn.sh might pass the event to systemd-logind. In that case you don't have to change anything in /etc/acpi/powerbtn.sh. You can change systemd configuration to do the shutdown. For this you edit /etc/systemd/logind.conf and you put into that file HandlePowerKey=poweroff Like this:
[Login]
# ...
HandlePowerKey=poweroff
# ...


Possible actions are "poweroff", "ignore", "reboot", "halt", "kexec", "suspend", "hibernate", "hybrid-sleep", and "lock". See "man logind.conf" for further details.

A simple physical shutdown button connected via USB

This is a project that I did first in the year 2000 when PCs had still physical RS232 interfaces and the power button was a hard power off without any acpi event. A lot of things have changed since then but this is still a pretty good option. Especially since it involved also an additional LED where you can see that the server is actually alive and running. This is very useful when there is no monitor attached. It is also good to have a separate button for shutdown which give you direct feedback. The power button, when pressed does not do anything immediately since it takes possibly 10-15 sec until the power LED goes really off.

This button is very easy to build. You need a USB to RS232 adapter, a LED, push button and a 1K resistor. The total cost is about 5 dollar US if you order the USB to RS232 adapter in china. Below are pictures and a circuit diagram explaining how to build this. The "driver" software, shutdown-button-rs232-0.1.tar.gz, for download at the end of this article, contains further instructions.

Note: You might have to test different USB to serial adapters. I have recently found some that seem to have only the Tx/Rx lines connected and do not support hardware flow control. All of them were adapters based on the CH341 chip. Try to use adapters with a PL2303 or a FT232 chip.

shutdown button shutdown button shutdown button shutdown button shutdown button shutdown button shutdown button shutdown button
The above button has a built-in LED. The button itself lights up when the server is up and running.

You can also buy a little USB adapter with just a board. Make sure it has the DTR and CTS pins. That is what we use. A lot of those small boards have only TX + RX pins and that will not be enough. We use the RS232 flow control pins for this project.
shutdown button shutdown button

RS232 pinout:

9 PIN-connector 25 PIN-connector Name Dir Description
1 8 CD input Carrier Detect
2 3 RXD input Receive Data
3 2 TXD output Transmit Data
4 20 DTR output Data Terminal Ready
5 7 GND -- System Ground
6 6 DSR input Data Set Ready
7 4 RTS output Request to Send
8 5 CTS input Clear to Send
9 22 RI input Ring Indicator

A web page to shutdown the server remotely

Here is a 3rd option to shutdown a server at home or inside a trusted zone. It uses a small web server written in perl and listening to port 88. The installation is simple. This is just one file plus some configuration files for systemd.

Of course you could use ssh and then the shutdown command but this is designed such that even people without any computer knowledge can do it.
shutdown button, http

All the software is available below.

References

Shutdown button usage

I have now been using the above solutions on my new server for a few month and here is my conclusion.

Normally we shutdown the server last. In the evening we power down the PCs and afterwards the server. That means the web based shutdown button is not too useful.

Using the power-on button for power off is confusing because it takes a time until the server shuts down. When you power the server on there is an LED that goes on immediately to indicate that the server is starting but there is no indication when you use this button to power it down.

The RS232 based button is the best. It gives feedback. You know if you pressed it properly and it is easy to use.
Back to: "No preservatives added"



© 2004-2022 Guido Socher