Skip to content

Short power press to sleep, long press to power off

Hi,

is it possible to configure your power button such that if you quickly press the power button it will put the computer into sleep/suspend mode, but if you press if for a little longer it will poweroff? I think it has something to do with /etc/system/logind.conf. Do you have any ideas? Thanks!

10 points3 comments
Open on Reddit
About this post
Posted
Archived
Score
10 · 99% up
Kind
Text
ID
lfmlju

Author

9
posts
25
comments
0
media
View profile

3 comments

You need to set it to HandlePowerKey=ignore in the logind.conf and then have your DE or WM to react to your button press. The button should be called something like "XF86PowerOff".

But keep in mind, that the power button often has a delay before sending an event, e.g. you have to hold it for a second until your systems gets the "XF86PowerOff" event. And often there is a hard shutdown, when you press it for a longer time (a couple of seconds). This functionality is in firmware and can't be turned of.

So you would need to find the sweet spots between pressing for one second and less than five seconds. I'd recommend a little script that checks for a button press. If it gets one, it waits a few seconds for another one. If nothing else happens, it suspends, if there is a second press it shuts down.

Edit: This might work. Save it somewhere, make it executable and have your DE run it wen XF86PowerOff is pressed:

#!/bin/bash
NAME=$(basename $0)
PID=$$
PIDFILE=/tmp/$NAME.pid

trap "rm -f $PIDFILE" EXIT

if pidof -o %PPID -x $NAME >/dev/null; then
        echo $PID > $PIDFILE
	sleep 5s
	exit 0
fi

while [ $SECONDS -lt 5 ]; do
  if [ -f "$PIDFILE" ]; then 
    OTHERPID=`cat $PIDFILE`
    kill $OTHERPID
    systemctl poweroff
    exit 0
  fi
done
systemctl suspend
exit 0

Basically: if run the first time, the script checks if there is a file in /tmp with the the name of the script. It checks this for five seconds, and if there is no file the script calls systemctl suspend and sends your system to sleep.

But in the beginning the script checks if there is another instance of itself running. So if you press the power button a second time (in about 5 seconds or less) it sees it is already running. Then it writes the file to /tmp. Now the first instance finds the file, kills the second process and calls systemctl poweroff.

6 points

I think that the DE handles that. Which are u using?

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.