How to soft kill gui applications via terminal?
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]`
Related videos on Youtube
David Craig
Updated on September 18, 2022Comments
-
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 over 12 yearsTake that one step frather and try
pkill -HUP [application_name]
:) -
tcoolspy over 12 yearsIf 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 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 over 12 yearsJust learned pkill and pgrep :D
-
David Craig over 12 yearsI 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 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 dopkill -HUP sed
. You will kill all the instances of sed you started plus any other instances doing useful work. -
Ruslan over 6 years@Caleb moreover,
pkill
will also kill some apps you don't expect to: e.g. if you havechromium
running, and want to kill a program calledrom
,pkill
will happily shootchromium
down. Better usekillall
, which will only match exact name (not on Solaris though, where it literally kills all processes). -
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 about 2 yearsif pkill would not be an option, this might help (to kill rhythmbox for example):
pid=$(ps -ae | grep rhythmbox | awk '{print $1}')