Difference between revisions of "Shutdown Pressing Power Button"

From ArchWiki
Jump to: navigation, search
(Second solution: power group is irrelevant)
(where I found my solution at least)
(7 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
[[uk:Shutdown Pressing Power Button]]
 
[[uk:Shutdown Pressing Power Button]]
 
[[zh-CN:Shutdown Pressing Power Button]]
 
[[zh-CN:Shutdown Pressing Power Button]]
== Prerequisites ==
+
{{stub|reason=should likely just be a redirect now}}
 
+
 
First of all, make sure that {{ic|button}} [[Kernel modules|module]] is loaded (check the output of {{ic|lsmod}}). If it is not, load it manually and set it to load at boot.
 
First of all, make sure that {{ic|button}} [[Kernel modules|module]] is loaded (check the output of {{ic|lsmod}}). If it is not, load it manually and set it to load at boot.
  
== systemd ==
+
{{ic|logind}} handles the event from pressing the power key, and the behaviour can be configured in {{ic|/etc/systemd/logind.conf}}. See https://wiki.archlinux.org/index.php/Systemd#ACPI_power_management for more information.
 
+
{{ic|logind}} handles the event from pressing the power key, and the behaviour can be configured in {{ic|/etc/systemd/logind.conf}}.
+
 
+
== sysvinit ==
+
{{Accuracy}}
+
If you want to shutdown your system by simply pressing the power button, do the following:
+
 
+
#Install the [[acpid]] package and add {{ic|acpid}} to the {{ic|DAEMONS}} array in [[Rc.conf#Daemons|/etc/rc.conf]].
+
#Create a file in {{ic|/etc/acpi/events/}} named {{ic|power}} with following content:
+
 
+
# /etc/acpi/events/power
+
# This is called when the user presses the power button
+
+
event=button/power (PWR.||PBTN)
+
action=/sbin/poweroff
+
 
+
To be able to test it, make sure the {{ic|acpid}} daemon is started or restart it if it is already launched, see [[Daemon]] for instructions. To test it without actually shutting down, comment out the {{ic|poweroff}} line and check your user/messages logs.
+
 
+
From now on, pressing the power button (lightly, not for a few seconds) should properly shutdown the system.
+
Note that if you have '''hibernate''' configured and working you may want to change the last line with:
+
action=/usr/sbin/hibernate
+
 
+
If you are using a more sophisticated [[Window Manager]], you should use its own shutdown call, so it would save its session etc.
+
 
+
=== KDE 3 ===
+
 
+
Change the action in {{ic|/etc/acpi/events/power}} to:
+
action=/opt/kde/bin/dcop --all-users --all-sessions ksmserver ksmserver logout 0 2 0
+
 
+
=== KDE 4 ===
+
 
+
As of KDE 4.4, you can still use dcop as shown above.
+
 
+
Alternatively, you can use {{Ic|PowerDevil}}:
+
# Delete (or comment out) {{Ic|/etc/acpi/events/power}}.
+
# Open System Settings.
+
# Go to Advanced>>Power Management.
+
# Select "Edit Profiles" and choose the current profile. (In KDE 4.4, the default profile is "Powersave.")
+
# Select "Shutdown" as the action for "When power button is pressed."
+
# Press Apply.
+
 
+
{{Note|
+
*With dcop and PowerDevil, the power button works ''only'' when KDE is running. Also, KDE needs to start from KDM (it probably also works when started from GDM). It does ''not'' work if you start KDE with a "startx" command.
+
*The PowerDevil configuration is ''per user''. To configure the power button for other users, repeat these steps for each user's account.}}
+
 
+
{{Expansion}}
+
{{Box YELLOW|TODO|Add simple multi-user configuration steps.}}
+
 
+
=== Xfce ===
+
 
+
For Xfce 4.4 change the action line to:
+
''action=echo POWEROFF | /usr/lib/xfce4/xfsm-shutdown-helper''
+
 
+
For Xfce 4.8 change the action line to:
+
''action=echo POWEROFF | /usr/lib/xfce4/session/xfsm-shutdown-helper''
+
 
+
{{Note|For a more robust solution (If you are facing frequent WM crashes or working on a sacrificial PC for developing or testing your software...), you should take a look at "/usr/src/linux/Documentation/sysrq.txt", which is a kernel facility for yielding you (the user...) the CPU so that it could be used for any '''rescue''' work.}}
+
 
+
=== SLIM and Xfce4 ===
+
 
+
To get the Xfce4 logout options window (logout, restart, shutdown, suspend, hibernate etc..) to appear when you hit the power button, you will need to do the following:
+
 
+
Create two scripts somewhere logical (/usr/local/sbin/ or something, it's really your choice):
+
 
+
'''xfce4-shutdown-as-user.sh:'''
+
#!/bin/sh
+
# xfce4-shutdown-as-user.sh
+
#
+
# Check how many users are currently logged into X.
+
# If only one user is logged in, run a script which sets
+
# up the environment to call xfce4-session-logout.
+
+
LOGGED_IN_USER=`ls -trl /tmp/.ICE-unix/ | tail -n -1 | cut -d' ' -f3`
+
NUM_X_USERS=`echo $LOGGED_IN_USER | wc -l`
+
+
if [ $NUM_X_USERS -eq 1 ]; then
+
    sudo -H -u $LOGGED_IN_USER /usr/local/sbin/xfce4-user-session-shutdown.sh
+
fi
+
'''xfce4-user-session-shutdown.sh:'''
+
#!/bin/sh
+
# xfce4-user-session-shutdown.sh
+
#
+
# Set up the environment so that DISPLAY and
+
# SESSION_MANAGER are valid, and xfce4-session-logout can run
+
+
HOST=`hostname`
+
SESSION_ID=`ls -t /tmp/.ICE-unix/ | head -n1`
+
# Note that distros use different syntax for the SESSION_MANAGER variable (this script has Debian's format).
+
# If this does not work, then try something like SESSION_MANAGER="local/$HOST:@/tmp/.ICE-unix/$SESSION_ID"
+
# instead. You can always find the correct format for your distribution by looking at the
+
# value of SESSION_MANAGER in a X session.
+
DISPLAY=:0.0 SESSION_MANAGER="local/$HOST:/tmp/.ICE-unix/$SESSION_ID,unix/$HOST:/tmp/.ICE-unix/$SESSION_ID" /usr/bin/xfce4-session-logout
+
 
+
Now map the acpi event PBTN to the script. There are a couple of ways of doing this. The example below is from '''/etc/acpi/handler.sh:'''
+
button/power)
+
        echo "PowerButton pressed!">/dev/tty5
+
        case "$2" in
+
            PBTN|PWRF)  logger "PowerButton pressed: $2"       
+
                        /usr/local/sbin/xfce4-shutdown-as-user.sh
+
                        ;;
+
        esac
+
        ;;
+
 
+
{{Accuracy|SLiM is now ConsoleKit capable, and using ck-launch-session to launch startxfce4 should disable some options in the logout window; the solution is simply using {{ic|exec startxfce4}} but it is not tested with this trick in particular.}}
+
 
+
In your '''~/.xinitrc''' file you will see a line similar to this:
+
exec ck-launch-session dbus-launch startxfce4
+
 
+
The dbus stuff is already handled by startxfce4 so you need to remove dbus-launch and any flags you may have so it reads:
+
exec ck-launch-session startxfce4
+
 
+
Log out and back in again and hit the power button.
+
 
+
== Second solution ==
+
 
+
#Install the [[acpid]] package and add {{ic|acpid}} to the {{ic|DAEMONS}} array in [[Rc.conf#Daemons|/etc/rc.conf]].
+
#Edit {{ic|/etc/acpi/handler.sh}}:
+
 
+
...
+
case "$1" in
+
    button/power)
+
        #echo "PowerButton pressed!">/dev/tty5
+
        case "$2" in
+
            PWRF)  logger "PowerButton pressed: $2"
+
    /sbin/poweroff;;
+
            *)      logger "ACPI action undefined: $2" ;;
+
        esac
+
        ;;
+
...
+
 
+
To be able to test it, make sure the {{ic|acpid}} daemon is started, see [[Daemon]] for instructions. To test it without actually shutting down, comment out the {{ic|poweroff}} line and check your user/messages logs.
+

Revision as of 20:14, 4 November 2012

Tango-document-new.pngThis article is a stub.Tango-document-new.png

Notes: should likely just be a redirect now (Discuss in Talk:Shutdown Pressing Power Button#)

First of all, make sure that button module is loaded (check the output of lsmod). If it is not, load it manually and set it to load at boot.

logind handles the event from pressing the power key, and the behaviour can be configured in /etc/systemd/logind.conf. See https://wiki.archlinux.org/index.php/Systemd#ACPI_power_management for more information.