How to soft kill gui applications via terminal?

17,721

Solution 1

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".

Solution 2

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.

Solution 3

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).

Solution 4

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.

Solution 5

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]`
Share:
17,721

Related videos on Youtube

David Craig
Author by

David Craig

Updated on September 18, 2022

Comments

  • David Craig
    David Craig over 1 year

    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.

  • tcoolspy
    tcoolspy over 12 years
    Take that one step frather and try pkill -HUP [application_name] :)
  • tcoolspy
    tcoolspy over 12 years
    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
  • David Craig
    David Craig over 12 years
    @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.
  • David Craig
    David Craig over 12 years
    Just learned pkill and pgrep :D
  • David Craig
    David Craig over 12 years
    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!
  • ahcox
    ahcox about 9 years
    @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.
  • Ruslan
    Ruslan over 6 years
    @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).
  • tcoolspy
    tcoolspy over 6 years
    @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.
  • Frank Nocke
    Frank Nocke about 2 years
    if pkill would not be an option, this might help (to kill rhythmbox for example): pid=$(ps -ae | grep rhythmbox | awk '{print $1}')