Way to catch shutdown event in Unity, Ubuntu 13.04

561 Views Asked by At

I'm developing gui application (C++/Qt) and now search for correct(documented) termination way for application. Is there any documentation how Unity DE notifies gui applications to exit (some analog of WM_QUERYENDSESSION signal on Windows)?

1

There are 1 best solutions below

1
On

From man 8 shutdown:

shutdown sends a request to the init(8) daemon to bring the system down into the appropriate runlevel. This is performed by emitting the runlevel(7) event, which includes the new runlevel in the RUNLEVEL environment variable as well as the previous runlevel (obtained from the environment or from /var/run/utmp) in the PREVLEVEL variable.

In other words, you can catch signal SIGTERM for your application, and upon receiving it, check current runlevel by reading /var/run/utmp and/or /var/log/utmp or executing runlevel command. If runlevel returns something like 2 0 (prevlevel runlevel), that means system is trying to switch from level 2 to level 0, that is, going to shutdown. This may allow you to have very short amount of time to react to current shutdown (but I don't think you can prevent it).