Linux Gnome GUI Shutdown Prompt

badwhitevision

Forerunner
TBH, This was the best sub-forum I could find to post this. I don't know if this falls into coding per se though.

So this one was a rollercoaster.

I wanted a pop-up prior to the system shutting down. This is normally done when you use Gnome GUI to power off.

poweroff.png


What happens in the backend is that "gnome-session-quit --power-off" is called and the GUI throws up a prompt asking you whether you want to shutdown and if no user response is received for 60 seconds, the device shuts down automatically.

I assumed, I can call this the same way from a systemd service.

However, systemd, threw errors about not being able to connect to an X11 display, despite providing Environment variables about the DISPLAY and XAUTHORITY.

Reading a SE post, I came across the fact that in Gnome GUI there is an autostart folder located at ~/.config/autostart where the .desktop file is run on start up.

So, I decided to use this to my advantage by writing a .desktop file that calls a script which runs a while loop of sleep every 30 seconds until the condition for shutdown is met. Once met, the script gets out of the loop and calls "gnome-session-quit".

Why do all this hullabaloo?

This system is being used as a HTPC in addition to being a backup server.

In the backup server case, the device turns on every single day at a specific time, runs the backup, then shuts down.

However, when being used as a HTPC, the system shutting down right when you're in the climax of a movie is not very nice. Hence, this script puts out a GUI confirmation dialog that the user can then interact with and cancel shutdown.

The best part is that this works, even when the display is not switched on. I believe the HDMI cable might need to be connected, but other than that, I do not think anything else is required.

Here's the .desktop autostart file and the script.

Code:
[Desktop Entry]

Name = UrBackup System Shutdown Script

Comment = Test for file in /var/urbackup. If present, delete the file and shutdown the system.

Exec = /home/user/Documents/Scripts/urbackup.sh

Terminal = false

Type = Application

X-GNOME-Autostart-enabled = true

Code for the script:
#!/bin/bash

while [ ! -f /var/urbackup/shutdown_now ];

do

sleep 30

done

# If file doesn't exist sleep for 30 seconds then redo loop.

#If file exists, get out of loop, then delete shutdown_now and finally start power-off attribute.

sudo -u urbackup rm -R /var/urbackup/shutdown_now

gnome-session-quit --power-off
Before any of you come at me arguing against using a HTPC as a backup server, etc, etc. This entity didn't have any kind of backup before. I'll take something over nothing anyday.

Also, if someone knows of a better way to optimise this, I'm all ears.

Hope this helps someone searching the inter-webs in the future. (but most probably will be fed into a LLM and then used.)
 
Last edited:
Instead of gnome-session-quit have you tried something like:

Code:
dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager.PowerOff boolean:true

So you can go back to using a systemd service file instead of looping in a script? (assuming this actually results in the popup in gnome)
 
@vishalrao

I tried it today finally. (Was running a badblocks that finished only today, hence the delay.)

Unfortunately, the command acts as equal to sudo shutdown now.

Changing the boolean value to false doesn't seem to change anything. The system still shuts down without warning.

Running the command without a boolean argument causes it to fail.

This is on Debian 12 with Gnome GUI.
 
Ah ok yeah it would be the same as shutdown now command...

Perhaps there is a different DBus service like org.gnome.SessionManager.xxx that will prompt in the UI...?
.

Something like this? (If it doesn't work, ensure wherever you call this, it's in the same user's context)

Code:
dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.EndSession

or

Code:
dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Shutdown
 
Last edited:
dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Shutdown


This one works as expected when calling it in a terminal, but it doesn't work when calling it via a service file, even after setting variables for the XAuth and DISPLAY and running it as the current user.

Similar to gnome-session-quit --power-off

Could you point me to your reference on these topics? I'd like to learn more about GNOME and the GUI.

Thanks for the continued help. :D
 
Sure, you're welcome - this also helps me learn this stuff haha.

For some reason I couldn't find the actual GNOME wiki or link where the actual docs for this DBUS API, just when I searched I got some random posts (like on stackexchange) that pointed to similar solutions.

Remember, for learning, your starting points should be searches on different platforms like Google, ChatGPT.com , perplexity.ai etc for best results.

As for the reason why it doesn't work via systemd service files I guess is because you need to run as the same user that is logged in - I think I read that too on one of the places I found.

If you can configure that service file to run as your user (if you search systemd service parameters, maybe there is an option to use specific username) then it might just work.

Good luck!
 
Usually, the systemd service file takes an argument in the [Service] section called User = where you give the username of the user you want to run the service as. This is what I tried. Let me bang my head on this the coming weekend and see if it can be worked out.

Will keep this thread updated.
 
Try running the full dbus command as part of the runuser command in your service file Exec param like the following?

Code:
runuser -l myusername -c 'dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Shutdown'
.

Alternatively, since you already tried specifying the User= option then did you ensure you installed the service file in the system folder like under /etc/systemd and not in your user home folders?
 
Back
Top