3

In my Ubuntu application, I wanted to perform certain finalization for my application before the system is being shutdown. For that, I need to identify the shutdown event in my application.

From what I have read online, there are mainly two major solutions, that are being suggested.

  1. using the 'PrepareForShutdown' dbus signal.
  2. trapping the SIGTERM signal that is send to the running applications prior to shutdown.

However, I have tried both the ways and they don't seem to work for me. The handler that I m registering for 'PrepareForShutdown' never gets invoked.

For method 2, I have observed that my application does not receive SIGTERM signal, instead it receives some other signal a moment before being terminated using SIGKILL.

Can someone help me out, what should I be using to identify the shutdown correctly? I do not intend to read it from certain OS files, please provide an event based solution.

1 Answers1

0

You can use libportal for that, there is a demo in Workbench (also available here: https://github.com/workbenchdev/demos/tree/main/src/Session%20Monitor%20and%20Inhibit)

Currently there are sample codes in Vala, Python and JavaScript; but you can port it to C++ to use with GTK or Qt.

Xut
  • 1
  • Well I could have posted an answer with Python too but the OP explicitly stated C. – WinEunuuchs2Unix Jun 17 '24 at 23:43
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Jun 22 '24 at 11:17