21

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.

11
  • How are you shutting down?
    – terdon
    Jun 13, 2016 at 9:41
  • 1
    Chromium and LibreOffice are not handling SIGTERM the way you'd like it to be handled Jun 13, 2016 at 9:49
  • 2
    I am not sure I understand what you mean. Do you mean that they are not handling SIGTERM correctly? Maybe they don't but as I wrote in my question I am working on a program that does handle SIGTERM but that code never seems to be run. SIGINT while pressing ctrl+C works perfectly. I still believe that there is some problem regarding Ubuntu notifying running processes on shut down.
    – GKraft
    Jun 13, 2016 at 9:52
  • 1
    Maybe related to this bug: #1448259 Systemd does not send SIGTERM first on shutdown ? Jun 13, 2016 at 10:44
  • 2
    It is annoying having to close all applications manually, and what if you forget a minimized one? It wont save your work. And there are lots of background processes that you either can't control easy or don't think about. They will not be given a chance to clean up after themselves or save whatever they were doing.
    – GKraft
    Jun 14, 2016 at 7:45

3 Answers 3

3

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.

1
  • I see the shutdown process and signal handling didn't work exactly as I thought. Now I understand how it is supposed to work and why it didn't work as I expected.
    – GKraft
    Aug 12, 2016 at 13:15
0

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.

2
  • I won't have access to my computer for a couple of days so i can't try it even if it is corrected. Should it really be necessary to use such a script? I think ubuntu shoukd handle it by itself.
    – GKraft
    Jun 29, 2016 at 14:58
  • Agreed. It works perfectly in Xubuntu, though. For instance, shutdown will wait indefinitely if there is an unsaved document.
    – S.R.
    Jul 10, 2016 at 14:47
0

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

1
  • They seem promising. I will probably end up using a similar script.
    – GKraft
    Aug 12, 2016 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .