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.
What happens in the backend is that “gnome-session-quit –power-off” is called and the GUI throws up a propmt 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
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.)