1

Systemd is configured by default to shutdown the computer when the power button is pressed. I want to keep this behavior, unless when my user is logged. When I'm logged in, I want to open a shutdown dialog instead of immediately shutting down.

I have achieved to disable systemd's handling using systemd-inhibit in a user service that starts when I log in and stops when I log out. This method works well, except that I have an idle thread for the call to sleep 1000d open all the time.

For cleanliness, I'm looking for a way to achieve this without the sleeping thread, which I find very annoying when I look at my htop.

Here is my user service:

[Unit]
Description=User daemon to disable systemd's handling of the power button

[Service]
ExecStart=nohup systemd-inhibit \
                --what="handle-power-key" \
                --who="disable-shutdown-button script" \
                --why="allows i3 to open shutdown-dialog instead" \
                sleep 1000d

[Install]
WantedBy=default.target

Edit: login is done through lightdm display manager.

1 Answer 1

1

Instead of making the systemd-inhibit command a separate user service, you might want to prefix it to the command that starts i3 or otherwise is essential to your GUI login session.

systemd-inhibit was designed to require a command to run it with, because the intent was to provide a way to inhibit things like suspend/hibernate/shutdown for the duration of specific tasks, like a package manager running a critical system update task, or burning an optical media, etc. That way, the inhibition can automatically end when the task that needed it ends or is aborted for any reason.

Adding systemd-inhibit to your session start-up that way can seem redundant because systemd/logind will track the processes belonging to your session via control groups, but that's the only way I see to make the usage pattern fit without requiring a dummy sleep command or similar.


In the comments you mentioned you use lightdm. It normally runs lightdm-session as the session-wrapper script. You could modify this script, or place an override file like this to /etc/lightdm/lightdm.conf.d/. You can choose the name for yourself, but it should have the suffix .conf:

[Seat:*]
session-wrapper=/usr/local/bin/shutdown-button-inhibit

And the /usr/local/bin/shutdown-button-inhibit script could be something like this:

#!/bin/sh
if [ $(whoami) = "Julien" ]  # edit this to match your username
then
    exec systemd-inhibit \
        --what="handle-power-key" \
        --who="shutdown-button-inhibit script" \
        --why="allows i3 to open shutdown-dialog instead" \
        lightdm-session "$@"
else
    exec lightdm-session "$@"
fi

You should set this script executable, with chmod 755 /usr/local/bin/shutdown-button-inhibit.

Using exec makes the script interpreter replace itself with lightdm-session instead of hanging around as one more process.

If your lightdm configuration is already customized to invoke something other than lightdm-session, then you'll have to edit this script to match.

4
  • I use lightdm as display manager, is there any way to have it wrap the call to i3 only for a specific user?
    – Julien__
    Commented Oct 19, 2019 at 10:10
  • See my edited answer above.
    – telcoM
    Commented Oct 19, 2019 at 13:17
  • I think it'd be better to use a group to disable the power button for any user in the group disable-powerbutton or similar. Thank you for the edit and detailed instructions, answer accepted.
    – Julien__
    Commented Oct 26, 2019 at 8:00
  • Sure, just replace the test in the if [ ... ] with a test for membership in a particular group and you've got it.
    – telcoM
    Commented Oct 26, 2019 at 8:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .