Ubuntu: Ubuntu is not sending SIGTERM on shutdown



Question:

I am beginning to suspect that Ubuntu is not telling the running applications that it is shutting down to let them quit correctly but instead forces them to quit.

If I leave chrome open when shutting down it says that it did not close correctly last time when I open it again after boot, LibreOffice does not ask me if I want to save my document and I am making an application which needs to run some code on exit but is not allowed to do so when the computer is shut down.

As I have understood it, SIGTERM is first sent to all processes to allow them to exit cleanly and if they don't exit SIGKILL is sent to force them to quit. It seems that Ubuntu is either not sending SIGTERM at all or does not give the applications enough time before sending SIGKILL.

Is there some way I can fix this?

I am running Ubuntu 16.04 but the problem existed in 15.10 as well. I can't tell if it was there before since I started using Ubuntu when 15.10 was the newest version.

Edit: I use Unity and shut down my computer by pressing the gear in the top right corner and selecting shut down, though the problem is the same if running sudo halt in the terminal.

Edit: I am observing the same behavior when only logging out. My guess is that the signal is supposed to be sent on log out and therefore the problem arises on shutdown and log out.


Solution:1

Unfortunately, it does not quite work this way. Many of those programs that do actually have a signal handler, don't block the signal to show a confirmation dialog. Signal handling is often minimal or missing.

You can easily try it without having to shutdown. Just send SIGTERM to your running Firefox or LibreOffice process:

$ pkill firefox  

Normally, Firefox might ask if you really want to close it. Also, it usually takes a while to close, especially when it's been running for a long time and using more than 1 GB of RAM - it often takes like a minute for the process to terminate after the its last window has been closed. None of this happens when you send SIGTERM, the process is just terminated instantly.

However, some desktop environments close all open windows before shutting down. Unlike SIGTERM, closing a window programmatically is like clicking on the X button of that window, or Alt + F4. This is also what the script in the other answer does - it uses wmctrl to close all open windows gracefully.

When a window is closed, the program is not in a rush and it can close everything within that window, for example open tabs, and it can also show a dialog window if there's unsaved work. Once all open windows of an application have been closed, the process usually terminates shortly after that.

So it depends on your desktop environment (assuming you use your desktop environment to shutdown and not sudo poweroff). For example, KDE should wait for open windows to be closed before shutting down, while MATE doesn't.

Bottom line: Close all open windows manually before shutting down. Or use a desktop environment that waits for all windows to be closed.


Solution:2

For what it's worth, some time ago, I have found this procedure on Manjaro forum:

close_apps () {  WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$|xfce4-panel$" | cut -f1 -d' ')  for i in $WIN_IDs; do wmctrl -ic "$i"; done    # Keep checking and waiting until all windows are closed  while [ "$WIN_IDs" != "" ]; do          sleep 0.1;          WIN_IDs=$(wmctrl -l | grep -vwE "Desktop$|xfce4-panel$" | cut -f1 -d' ')  done  

}

You can call it in a script of your own to close all open apps prior to shutdown(or restart).

The only problem being, it was created for Xfce so, it needs changes that Unity DE imposes. A little help from somebody more knowledgeable would come in nicely here to help solve OP's problem.


Solution:3

Actually, a part of script that closes all windows and doesn't depend on any particular DE is available on Ask. 2-nd and 3-rd answers are particularly useful. With just few lines more, and by creating a launcher in panel or on desktop you can achieve the gracefull shutdown that works in the DE you're using.

How to close all open windows gracefully


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »