Configuring the power button in Gnome

As many Gnome users know, since Gnome Shell 3.18, the option to power down (much less prompt the user) by pressing the power button has been removed by the developers, now only allowing either Suspend, Hibernate, or simply to ignore the button press. While existing workarounds for this include using systemd to ungracefully shut the system down (no prompts), set a separate keyboard shortcut to achieve the same behaviour, or re-implementing the functionality manually in gnome-settings-daemon, I found these unnecessarily complicated or lacking value. Instead, I have developed my own method, which properly maps the power button to prompt the user to shutdown, reboot, or simply cancel.Firstly, a brief overview of what this method does. Firstly, a startup application is added to save certain environment variables. This allows the correct user and display to be prompted for shutdown. Next, the acpi event for the button press is modified, and a script is executed to run the prompt. This method was developed by combining suggestions from here, here and here.

1. Disable Systemd and Gnome handlers

Depending on the Linux distribution used, desktop environment, and current configuration, systemd may be set to handle the event for the power button press. In order for this method to work (with acpi), this must be disabled. In the /etc/systemd/logind.conf file, edit/set the following:

HandlePowerKey=ignore

If Gnome is set to suspend or hibernate the system when the button is pressed, this must be disabled. This can be achieved by changing the When the Power Button is pressed action in the Power menu for system settings, to Nothing. Alternatively, the following command may be used:

gsettings set org.gnome.settings-daemon.plugins.power power-button-action 'nothing'

2. Setup the shutdown script

In this example, the script in question is being placed in /etc/acpi/shutdown.sh however this may be changed upon user preference. The script is as follows:


#!/bin/bash
#Script to simulate normal power button actions with gnome
#Case 1 is launched from a startup script, to save the session info
#Case 2 loads the session info to prompt the user for powering down
case $1 in
	1)
		mkdir ~/tmp 2> /dev/null
		echo DISPLAY=$DISPLAY > ~/tmp/gnome-session
		echo SESSION_MANAGER=$SESSION_MANAGER >> ~/tmp/gnome-session
		echo XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR >> ~/tmp/gnome-session
	;;
	2)
		gnome_session_user=$(ps -o command,user -u gdm,root -N | grep gnome-session | awk '{print $2}')
		export $(cat /home/$gnome_session_user/tmp/gnome-session)
		sudo -E -u $gnome_session_user gnome-session-quit --power-off
esac

Once saved, be sure to give anyone execution permissions (i.e. run chmod +x script). Also note, if a different Display Manager is used instead of GDM, line 13 may need to be modified to reflect this.

3. Setup startup script

In order for the script to work, certain environment variables must be stored per user, which can later be retrieved. The code for both of these steps is provided in the previous script, for convenience. Create a file in ~/.config/autostart/shutdown.desktop with the following contents:

[Desktop Entry]
Name=SessionSaver
GenericName=Session Startup Saver
Exec=/etc/acpi/shutdown.sh 1
Terminal=false
Type=Application
X-GNOME-Autostart-enabled=true

Note that, if a different location is used for the previous script, it should be modified here in the Exec box. Also note, this file will need to be added to the autostart directory for every user on the system who desires to have the button functional.

4. Modify ACPI event handler

Again, due to different configurations, this step may vary slightly. If no special handler has already been configured for the power button, acpid will look in /etc/acpi/handler.sh for necessary actions. Otherwise, some configurations may use custom events in the events directory, or have a script named (for example) powerbtn.sh to handle this. Regardless, look for a script running an event (e.g. logger ‘PowerButton pressed’) for the power/button action, or the PBTN key press. To this, add the following line after the logger:

/etc/acpi/shutdown.sh 2

To test, simply restart the acpid service, and run the needed command to save the environment variables:

sudo systemctl restart acpid

/etc/acpid/shutdown.sh 1

Assuming all works as expected, pressing the power button should now display the prompt to shutdown or reboot the system.

Advertisements

2 thoughts on “Configuring the power button in Gnome

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s