22

Is there a way to close a GUI application in friendly "please quit yourself now" way, without graphical access to the applications window?

For example, if Gnome/X display crashes to black, I'd like to switch to tty2 and close applications like firefox in a way which lets them save their config etc. At best without further user queries.

19

Usually, you can use SIGHUP to "friendly" close an application (with or without graphical interface).

kill -HUP <application_pid>

EDITED: added some other info

The way SIGHUP is handled is application dependent so, as Dave noted, it can happen that this signal is masked or handled. However quite all interactive applications exit gracefully with a SIGHUP.

On the other side, I have to admit that usually I follow this schema:

kill -HUP <application_pid>
# check if application is still running
kill -INT <application_pid>
# check if application is still running
kill -KILL <application_pid>

Obviously the last command is not so "friendly".

  • 3
    If you don't know the PID of the application you can use pkill to specify the name of the executable instead. For example: pkill -HUP firefox-bin – Caleb Aug 26 '11 at 15:13
  • @Dave too: Interesting, I found this related which suggests a similar but reverted order: TERM->INT->HUP (mind the old age). Maybe I will keep HUP->INT->TERM in mind, as KILL doesn't seem to be friendly to me. Thanks to all. – jfk Aug 29 '11 at 9:03
  • @Caleb, just be careful there where there are multiple processes with the same name: pkill will kill them all. E.g., run this a few times to make some background processes: sed "s/Woo/woo/" < /dev/urandom >> /dev/null &. Then do pkill -HUP sed. You will kill all the instances of sed you started plus any other instances doing useful work. – ahcox Apr 7 '15 at 11:54
  • @Caleb moreover, pkill will also kill some apps you don't expect to: e.g. if you have chromium running, and want to kill a program called rom, pkill will happily shoot chromium down. Better use killall, which will only match exact name (not on Solaris though, where it literally kills all processes). – Ruslan Jan 5 '18 at 15:53
  • @Ruslan Or use pkill -x to get an exact match instead of a partial match. I personally take advantage of the partial matching to save typing, but you have to know what the tool does. Yes, by default it matches partial strings. Another useful option is -f to match against the full command line instead of just $0. – Caleb Jan 5 '18 at 15:57
5

You may want to try:

kill -TERM <pid>

or

kill -TERM `pidof <application_name>`

At least on some systems, when the system is going through shutdown, all processes get sent the TERM (15) signal before they get sent the KILL (9) signal. HUP often just causes the application to reload its configs, but it can be different on a process-by-processes basis.

2

There's no general way to tell an application to exit and save its configuration. There may be ways to notify applications using a particular framework, e.g. dcop kfoo MainApplication-Interface quit for some KDE 3 applications. Mind that the application might show a dialog box asking you for confirmation.

If the X display becomes unusable, kill the X server. Press Ctrl+Alt+BackSpace; if that doesn't work, kill the X server process with sudo pkill -x Xorg. When the X server dies, all X applications are notified and they will try to exit gracefully (there's no guarantee that they will save everything, but it's your best bet).

  • I didn't know if the apps will be notified or just killed; and as the key combo isn't enabled by default on Fedora..... I now know how to kill it by command line! – jfk Aug 29 '11 at 9:13
1

Akin to @andcoz's answer, if you don't know the application's PID (I'm assuming you don't know it off by heart), try this:

kill -HUP `pidof [application_name]`
  • 2
    Take that one step frather and try pkill -HUP [application_name] :) – Caleb Aug 26 '11 at 15:13
  • Just learned pkill and pgrep :D – jfk Aug 29 '11 at 9:07
1

Two other commands you might want to consider are wmctrl -c and xdotool windowkill. I'm not sure whether windowkill is exactly what you're after, if not you might be interested in the windowdelete patch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.